【问题标题】:Unable to obtain token using Abraham's TwitterOAuth for PHP; HTTP 500 returned无法使用 Abraham 的 TwitterOAuth for PHP 获取令牌;返回 HTTP 500
【发布时间】:2015-07-20 18:07:48
【问题描述】:

我正在尝试为 PHP 使用 @abraham's TwitterOAuth 0.5.3 库,但是当我请求为 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 帐户。

我做错了什么?

【问题讨论】:

    标签: php twitter oauth twitter-oauth


    【解决方案1】:

    我的问题以另一种方式得到解决。在检查(根据 jhenderson2099 回答)我的主机启用了 curl_exec 之后(确实如此)。我发现我的问题是由 src/TwitterOauth.php(TwitterOauth 类)中的两行引起的:

    $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,
            ];
    

    【讨论】:

      【解决方案2】:

      事实证明,从来没有得到回应。结果,尝试处理响应,但没有响应会导致服务器端出错。

      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,而我没有意识到这一点。此外,我使用 Twitter API 的调用在过去一直有效,所以这是新行为。

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

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

      然而事实并非如此。

      因此,如果您在使用此库时遇到问题(过去它对我非常有用),可能是您的托管服务提供商禁用了 curl_exec

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

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

      【讨论】:

        猜你喜欢
        • 2012-05-18
        • 2022-01-03
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 2016-08-06
        • 1970-01-01
        • 2015-06-28
        相关资源
        最近更新 更多