2012-05-15 59 views
1

我想使用此代碼連接到我們的客戶關係管理平臺:Desk.com OAuth的問題在PHP

https://gist.github.com/2564090

但它在第28行徹底失敗:

$的OAuth =新OAuth($ consumer_key,$ consumer_secret);

有什麼我需要做的實例化一個新的OAuth對象?我應該指的是一些圖書館或包含文件?或者我必須在我的php.ini配置文件中啓用某些內容?爲什麼'新OAuth()'不適合我?我所得到的是:500 - 內部服務器錯誤。

+0

你按照指示保存在人的上方文件?您是否在安裝了OAuth Extension的情況下使用PHP 5.4? – 2012-05-15 20:01:46

+0

使用php v5.3.10。 oauth沒有預裝? – bobbiloo

+0

沒關係...我發現它: – bobbiloo

回答

0

你需要從PECL安裝的OAuth例如

2

正如保羅所說,你需要安裝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代碼片段,我希望這個代碼將一段時間