Desk.com PHP 中的 OAuth 问题


Desk.com OAuth problems in php

我正在尝试使用以下代码连接到我们的CRM平台:

https://gist.github.com/2564090

但它在第 28 行彻底失败了:

$oauth = new OAuth($consumer_key, $consumer_secret(;

我需要做些什么来实例化一个新的 OAuth 对象吗?我应该指的是某个库还是包含文件?还是我必须在我的 php.ini 配置文件中启用某些内容?为什么"new OAuth(("对我不起作用?我得到的只是:500 - 内部服务器错误。

正如Paul提到的,你需要安装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 片段,我希望这段代码能为某人节省一些时间

例如

,您需要从 pecl 安装 OAuth