Twitter API总是返回false

时间:2014-11-03 14:43:05

标签: php api twitter twitter-oauth

我尝试使用此opensource API发布推文。我创建了Twitter App并收到了所有需要的密钥(4个密钥)。所以我不明白为什么我的剧本错了。

ini_set('display_errors', 1);
require_once("libs/twitter/TwitterAPIExchange.php");

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "...",
    'oauth_access_token_secret' => "...",
    'consumer_key' => "...",
    'consumer_secret' => "..."
);

$url = "https://api.twitter.com/1.1/statuses/update.json";
$requestMethod = 'POST';

/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
    'status' => 'Hi, I am new status from Social Poster!'
);

/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();
var_dump($result);

var_dump显示 false

1 个答案:

答案 0 :(得分:1)

我知道这是一个老帖子,但只是对于遇到同样问题的人来说,这就是解决方案:

TwitterAPIExchange.php文件中,转到performRequest方法并从

更改$options变量
    $options = array( 
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_HEADER => false,
        CURLOPT_URL => $this->url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 10,
    );

到此:

    $options = array( 
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_HEADER => false,
        CURLOPT_URL => $this->url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false, //this is what you need to add
        CURLOPT_TIMEOUT => 10,
    );