无法使用 Abraham 的 TwitterOAuth 为 PHP 获取令牌;返回 HTTP 500

Unable to obtain token using Abraham's TwitterOAuth for PHP; HTTP 500 returned

我正在尝试使用 @abraham's TwitterOAuth 0.5.3 library for PHP, but when I make a request to request a token for the 3-legged authorization,我收到 HTTP 500 作为响应。

以下是我在 PHP 中设置代码的方式:

<?php

/* Start session and load library. */
session_start();

require_once('config.php');

require_once('twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;

/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

/* Get temporary credentials. */
// Error occurs on the following line, unable to dump $request_token 
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));

//print_r($request_token); // <-- Never reached!!

我知道这个问题不在 Twitter API 中,因为我已经确认我可以通过 Dev console.

访问我的 Twitter 帐户

此外,我已经在某种程度上验证了 TwiterOAuth 库是否正常工作,方法是遵循库提供的 Authorization flow example。该示例还可以访问我的 Twitter 帐户。

我无法弄清楚发生了什么,因为我无法正确授权我的 PHP 应用程序访问我的 Twitter 帐户。

我做错了什么?

原来一直没有得到回应。结果,在 none 时尝试处理响应导致服务器端出错。

Twitter OAuth 所依赖的 PHP 功能之一是 curl。我已经测试过 curl_init 是否存在:

print function_exists('curl_init') ? 'curl_init is enabled' : 'curl_init is disabled';

而且我错误地认为 curl_exec 也已启用。 (为什么要启用 curl_init,而只禁用 curl_exec?)

该假设不正确,因为我的网络托管服务提供商已禁用 curl_exec "due to security concerns" 而我并不知道这一点。此外,我使用 Twitter API 的呼吁在过去一直奏效,所以这是新行为。

我花了一段时间才回来测试 curl_exec。我确认我正在接收一个有效的 TwitterOauth 对象,并最终进入 TwitterOauth class 和请求函数。

我没有收到卷曲错误,但是 curl_exec 的响应是否为空(而不是 TRUE or FALSE as expected)。我认为这很不寻常,起初以为 curl 缺少配置选项。

然而,事实并非如此。

因此,如果您 运行 遇到此库的问题(过去对我来说效果很好),可能是您的托管服务提供商禁用了 curl_exec

您可以通过以下 PHP 代码测试此场景:

print function_exists('curl_exec') ? 'curl_exec is enabled' : 'curl_exec is disabled';

我的问题已通过其他方式解决。在检查(根据 jhenderson2099 的回答)我的主机启用了 curl_exec 之后(确实如此)。我发现我的问题是由 src/TwitterOauth.php (TwitterOauth class) 中的两行引起的:

$bundlePath = CaBundle::getSystemCaRootBundlePath(); <-- Comment this line
$options = [
        // CURLOPT_VERBOSE => true,
        CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
        CURLOPT_HEADER => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_TIMEOUT => $this->timeout,
        CURLOPT_USERAGENT => $this->userAgent,
        $this->curlCaOpt($bundlePath) => $bundlePath,<-- Comment this line
    ];

这样您的代码将如下所示:

//$bundlePath = CaBundle::getSystemCaRootBundlePath();
        $options = [
            // CURLOPT_VERBOSE => true,
            CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_TIMEOUT => $this->timeout,
            CURLOPT_USERAGENT => $this->userAgent,
            //$this->curlCaOpt($bundlePath) => $bundlePath,
        ];