无法使用PHP获得扩展推文

时间:2017-11-30 10:11:51

标签: php twitter php-curl

我有一个已经运行的Twitter应用程序,我用它来使用PHP CURL在我的网站上显示时间线。现在我想使用tweet_mode=extended来获得280个字符,但它说的是身份验证问题。

以下是我的代码:

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "XXXXX";
$oauth_access_token_secret = "XXXXX";
$consumer_key = "XXXXX";
$consumer_secret = "XXXXX";

$oauth = array(
            'screen_name' => 'MAhmadZ',
            'count' => 4,
            'oauth_consumer_key' => $consumer_key,
            'oauth_nonce' => time(),
            'oauth_signature_method' => 'HMAC-SHA1',
            'oauth_token' => $oauth_access_token,
            'oauth_timestamp' => time(),
            'oauth_version' => '1.0'
        );

//build the base string
$temp = array();
ksort($oauth);
foreach($oauth as $key => $value):
    $temp[] = "$key=" . rawurlencode($value);
endforeach;
$base_info = 'GET&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $temp));
unset( $temp );

$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, TRUE));
$oauth['oauth_signature'] = $oauth_signature;

//build authorization header for making request
$temp = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value):
    $values[] = "$key=\"" . rawurlencode($value) . "\"";
endforeach;

$temp .= implode(', ', $values);
$header = array($temp, 'Expect:');
unset( $temp );

$options = array(
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_HEADER => FALSE,
            CURLOPT_URL => $url . '?screen_name=MAhmadZ&count=4',
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_SSL_VERIFYPEER => FALSE
        );

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

1 个答案:

答案 0 :(得分:1)

使用'tweet_mode = extended'获取扩展/完整的tweet 转到此处了解更多详细信息:https://www.finalwebsites.com/twitter-search-api/

我猜这是CURL的问题。使用此库来避免错误: https://github.com/J7mbo/twitter-api-php