简体   繁体   English

无法使用亚伯拉罕的 TwitterOAuth for PHP 获取令牌; 返回 HTTP 500

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

I am attempting to use @abraham's TwitterOAuth 0.5.3 library for PHP, but when I make a request to request a token for the 3-legged authorization , I receive an HTTP 500 as a response.我正在尝试将@abrahamTwitterOAuth 0.5.3 库用于 PHP,但是当我发出请求以请求3 条腿授权的令牌时,我收到了 HTTP 500 作为响应。

Here is how I have the code set up in PHP:这是我在 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!!

I know that this problem is not within the Twitter API, as I have verified that I can access my Twitter account via the Dev console .我知道这个问题不在 Twitter API 中,因为我已经验证我可以通过开发控制台访问我的 Twitter 帐户。

In addition, I have verified to some degree that the TwiterOAuth library is working, by following the Authorization flow example provided with the library.此外,通过遵循库提供的授权流程示例,我在某种程度上验证了 TwiterOAuth 库是否正常工作。 The example can also access my Twitter account.该示例还可以访问我的 Twitter 帐户。

I just can't figure out what is going on as I am unable to properly authorize my PHP application to have access to my Twitter account.我只是无法弄清楚发生了什么,因为我无法正确授权我的 PHP 应用程序访问我的 Twitter 帐户。

What am I doing wrong?我究竟做错了什么?

It turns out that a response was never obtained.事实证明,从未获得响应。 As a result, attempting to process a response, when there was none resulted in errors on the server side.结果,尝试处理响应,当没有响应时会导致服务器端出现错误。

One of the PHP functions that Twitter OAuth relies upon is curl. Twitter OAuth 依赖的 PHP 函数之一是 curl。 I had tested to see if curl_init existed:我已经测试过curl_init存在:

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

and I erroneously assumed that curl_exec was also enabled.我错误地认为curl_exec也已启用。 (Why would you leave curl_init enabled, but only disable curl_exec ?) (为什么要启用curl_init ,而只禁用curl_exec ?)

That assumption was incorrect as my web hosting provider has disabled curl_exec "due to security concerns" and I was unaware of this.这个假设是不正确的,因为我的网络托管服务提供商“出于安全考虑”禁用了curl_exec而我不知道这一点。 In addition, my call to use the Twitter API has worked in the past, so this was new behavior.此外,我对使用 Twitter API 的调用在过去一直有效,因此这是新行为。

It took me a while to come back to testing curl_exec .我花了一段时间才回来测试curl_exec I verified that I was receiving a valid TwitterOauth object and eventually wound my way into the TwitterOauth class and into the request function.我确认我正在接收一个有效的 TwitterOauth 对象,并最终进入 TwitterOauth 类和请求函数。

I was receiving no curl error, but was the response from curl_exec was null (not TRUE or FALSE as expected ).我没有收到 curl 错误,但是curl_exec的响应curl_exec为空(不是预期的 TRUE 或 FALSE )。 I thought that this was unusual and at first thought that curl was missing a configuration option.我认为这是不寻常的,起初以为 curl 缺少配置选项。

However, it was not.然而,事实并非如此。

So, if you run into problems with this library (which has worked great for me in the past), it may be that your hosting provider disabled curl_exec .因此,如果您遇到此库的问题(过去对我来说非常curl_exec ),则可能是您的托管服务提供商禁用了curl_exec

You can test this scenario via the following PHP code:您可以通过以下 PHP 代码测试此场景:

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

My problem was fixed in another way.我的问题以另一种方式解决了。 After checking (as per jhenderson2099 answer) that my hosting had curl_exec enabled (which it did).在检查(根据 jhenderson2099 的答案)我的主机是否启用了 curl_exec 之后(确实如此)。 I found out that my problem was caused by two lines in src/TwitterOauth.php (TwitterOauth class):我发现我的问题是由 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
    ];

so that your code will look like this:这样您的代码将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM