问题
I am using the Twitter API to create a web application. However, I would like to be able to get a list of users that a specific user is following. I have looked at the documentation and I have not been able to find how to do this.
For example, I might be trying to get the users that baconman
is following. How can I do this using the Twitter API?
回答1:
<?php
$screen_name = 'baconman';
$url = 'https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name='.$screen_name;
$list = curl($url,'GET');
$followers_ids = json_decode($list);
$user_detail = 'https://api.twitter.com/1/users/lookup.json?user_id='.implode(',',$followers_ids->ids).'&include_entities=true';
$details = curl($user_detail,'GET');
function curl($url, $method = 'get', $header = null, $postdata = null, $includeheader=FALSE, $timeout = 60)
{
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
if ($header)
curl_setopt($s,CURLOPT_HTTPHEADER, $header);
/*if ($this->debug)*/
curl_setopt($s,CURLOPT_VERBOSE, FALSE);
curl_setopt($s,CURLOPT_TIMEOUT, $timeout);
curl_setopt($s,CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($s,CURLOPT_MAXREDIRS, 3);
curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
curl_setopt($s,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($s,CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($s,CURLOPT_COOKIEFILE, 'cookie.txt');
if(strtolower($method) == 'post')
{
curl_setopt($s,CURLOPT_POST, true);
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
}
else if(strtolower($method) == 'delete')
{
curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'DELETE');
}
else if(strtolower($method) == 'put')
{
curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
}
curl_setopt($s,CURLOPT_HEADER, $includeheader);
//curl_setopt($s,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $html;
}
来源:https://stackoverflow.com/questions/15964772/get-users-a-twitter-user-is-following