正如保羅所說,你需要安裝PECL擴展http://it2.php.net/oauth
一旦你的擴展安裝,你可以創建像我寫的方法連接到API。 正如你所看到的,我定義了一個存儲在我命名爲Desk_Client的Client類中的私有方法。 如果您需要執行多個請求,最好在類構造函數中移動oAuth對象創建並將其存儲到實例變量中。
const API_URL = "https://YOURSITE.desk.com/api/v2/";
// Access token & secret (Click [Your Access Token] on App Listing)
// https://[yoursite].desk.com/admin/settings/api-applications)
const ACCESS_TOKEN = "*****";
const ACCESS_SECRET = "*****";
// Application key and secret found here:
// https://[yoursite].desk.com/admin/settings/api-applications
const CONSUMER_KEY = "*****";
const CONSUMER_SECRET = "*****";
/**
* Utility method to perform a request to the Desk.com API.
*
* @param string $actionPath - The relative path to an API action (e.g. companies)
* @param array $params - Array containing key value parameters for the request
* @param string $request - POST, GET, PUT, PATCH
* @return Array
*/
private function _performRequest($actionPath, $params = null, $request = OAUTH_HTTP_METHOD_GET) {
$url = self::API_URL.$actionPath;
try {
$oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET);
$oauth->setToken(ACCESS_TOKEN, ACCESS_SECRET);
$oauth->disableSSLChecks();
switch ($request) {
case OAUTH_HTTP_METHOD_GET:
// Add get params to the url.
$url .= ($params && $request === "GET") ? "?".http_build_query($params) : "";
$oauth->fetch($url);
break;
case OAUTH_HTTP_METHOD_POST:
$oauth->fetch($url, json_encode($params), OAUTH_HTTP_METHOD_POST);
break;
default:
$oauth->fetch($url, json_encode($params), OAUTH_HTTP_METHOD_PUT);
}
$result = $oauth->getLastResponse();
}
catch(Exception $e) {
error_log("Error: ".$e->getCode()." - ".$e->getMessage());
}
return json_decode($result);
}
我分享這個代碼,因爲desk.com API文檔提供只是一個樣本的Ruby代碼片段,我希望這個代碼將一段時間
你按照指示保存在人的上方文件?您是否在安裝了OAuth Extension的情況下使用PHP 5.4? – 2012-05-15 20:01:46
使用php v5.3.10。 oauth沒有預裝? – bobbiloo
沒關係...我發現它: – bobbiloo