Php 如何在Twitter API 1.1中获得追随者数量?
我正在尝试使用API版本1.1从Twitter上的X用户那里获取追随者数量 我使用的代码很好用。我需要的是一个函数,从Twitter提供给我的数据中获取我需要的特定数据。在这种情况下,我需要Php 如何在Twitter API 1.1中获得追随者数量?,php,json,twitter,twitter-oauth,Php,Json,Twitter,Twitter Oauth,我正在尝试使用API版本1.1从Twitter上的X用户那里获取追随者数量 我使用的代码很好用。我需要的是一个函数,从Twitter提供给我的数据中获取我需要的特定数据。在这种情况下,我需要['followers\u count'] 以下是PHP代码: <?php $token = '*'; $token_secret = '*'; $consumer_key = '*'; $consumer_secret = '*'; $host = 'api.twitter.com'; $metho
['followers\u count']
以下是PHP代码:
<?php
$token = '*';
$token_secret = '*';
$consumer_key = '*';
$consumer_secret = '*';
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/users/lookup.json'; // api call path
$query = array( // query parameters
'screen_name' => 'ArianaGrande',
'count' => '5'
);
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_token' => $token,
'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0'
);
$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);
$arr = array_merge($oauth, $query); // combine the values THEN sort
asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)
// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));
$url = "https://$host$path";
// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);
$url=str_replace("&","&",$url); //Patch by @Frewuill
$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it
// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);
// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json, true);
echo "<pre>" . print_r($twitter_data, true) . "</pre>";
?>
我本应该向您展示如何格式化
print\r()
,这样输出的可读性(要多得多!)。下面是如何做到这一点
$followers_count = $twitter_data[0]->followers_count;
了解如何从调试中解决这一问题很重要。首先,print\r
的输出表明返回的对象是一个数组,第一个条目是一个零索引,看起来它是在列出搜索结果(基本上是)
第0项是一个对象(它是一个stdClass
),不是数组,因此需要使用引用->
操作符而不是数组索引来查找要检索的对象。我猜每个搜索结果的所有数据项都可以作为该对象的属性值进行查找
另一方面,您会注意到此对象中的某些项也是对象。您可以使用其他参考来查找这些内容,例如:
是的,JSON响应包含您需要的,
followers\u count
。那么,用json\u decode
解码它,然后查看您得到的对象/数组?如果你print\r($twitter\u data),你会得到什么代码>?请将其编辑到您的问题中。谢谢@halfer,我添加:$followers\u count=$twitter\u data[0]['followers\u count';并得到以下结果:致命错误:无法将stdClass类型的对象用作数组inNo@user3307502。我已经从你的问题中删除了答案,因为我们喜欢这里保留的问答格式。您可以随时为自己的问题添加自己的答案,但我认为在这种情况下没有必要这样做-它包含在我的答案中。此外,关于数组和对象结构的内容确实值得一读。给定一个print\r()
输出,通常需要对其进行检查,并确定查找特定项所需的符号集。我将阅读有关数组和结构的更多信息。再次谢谢你!。
echo "<pre>" . print_r($twitter_data, true) . "</pre>";
$followers_count = $twitter_data[0]->followers_count;
$twitter_data[0]->entities->url;