Sugarcrm支持 产品指南 糖开发人员 糖开发人员指南11.0 食谱 网页服务 休息API. PHP. 如何获取当前用户的时间

如何获取当前用户的时间

概述

A PHP example demonstrating how to fetch the current users time with the v11 /ping/whattimeisit REST GET endpoint.

验证

首先,您需要对Sugar API进行身份验证。 一个例子如下所示:

<?php

$instance_url = "http://{site_url}/rest/v11";
$username = "admin";
$password = "password";

//Login - POST /oauth2/token
$auth_url = $instance_url . "/oauth2/token";

$oauth2_token_arguments = array(
    "grant_type" => "password",
    //client id - default is sugar. 
    //It is recommended to create your own in Admin > OAuth Keys
    "client_id" => "sugar", 
    "client_secret" => "",
    "username" => $username,
    "password" => $password,
    //platform type - default is base.
    //It is recommend to change the platform to a custom name such as "custom_api" to avoid authentication conflicts.
    "platform" => "custom_api" 
);

$auth_request = curl_init($auth_url);
curl_setopt($auth_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($auth_request, CURLOPT_HEADER, false);
curl_setopt($auth_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($auth_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($auth_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($auth_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json"
));

//convert arguments to json
$json_arguments = json_encode($oauth2_token_arguments);
curl_setopt($auth_request, CURLOPT_POSTFIELDS, $json_arguments);

//execute request
$oauth2_token_response = curl_exec($auth_request);

//decode oauth2 response to get token
$oauth2_token_response_obj = json_decode($oauth2_token_response);
$oauth_token = $oauth2_token_response_obj->access_token;

有关验证的更多信息可以在其中找到 如何进行身份验证和注销 示例和 / oauth2 / logout   端点文档。

获取用户的当前时间和日期

//Set up the time parameters - GET /ping/whattimeisit

$time_url = $instance_url . "/ping/whattimeisit";

$time_request = curl_init($search_url);
curl_setopt($time_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($time_request, CURLOPT_HEADER, false);
curl_setopt($time_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($time_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($time_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($time_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//execute request
$time_response = curl_exec($time_request);

//decode json
$time_response_obj = json_decode($time_response);

要求

http://{site_url}/rest/v11/ping/whattimeisit

回复

"2014-04-08T14:59:13-04:00"

下载

您可以下载完整的API示例 这里

最后修改:2021-02-17 02:44:13