有没有一种快速的方法在 PHP 或 JavaScript 中获取 Twitter 账户的头像?我需要获取完整图片的 URL(而不是头像大小)。谢谢。任何代码示例都可以。
有没有一种快速的方法在 PHP 或 JavaScript 中获取 Twitter 账户的头像?我需要获取完整图片的 URL(而不是头像大小)。谢谢。任何代码示例都可以。
Twitter有一个简单易记的URL。
https://api.twitter.com/1/users/profile_image/abraham
它具有像"?size=bigger"这样的尺寸选项。
您可以在Little known Twitter and TwitterAPI tips and tricks中了解更多信息。
Twitter现在有文档作为GET users/profile_image/:screen_name。
更新:此方法已从API v1.1中删除。推荐做法是使用GET /users/show并在服务/应用程序中本地缓存profile_image_url
。
function get_big_profile_image($username, $size = '') {
$api_call = 'http://twitter.com/users/show/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
return str_replace('_normal', $size, $results->profile_image_url);
}
使用get_big_profile_image('bobsaget', '_bigger')应返回一个大头像:http://a1.twimg.com/profile_images/330305510/n229938150541_9850_bigger.jpg
而使用get_big_profile_image('bobsaget')则应返回一个更大的图像:http://a1.twimg.com/profile_images/330305510/n229938150541_9850.jpg
如果这已经是众所周知的事情,那我很抱歉,但在我的搜索中,包括官方的Twitter文档,都没有找到相关记录。
你可以添加参数?size=original,这将返回用户上传的原始图像。
因此,以下链接将返回原始图像:
http://api.twitter.com/1/users/profile_image/twitter.json?size=original
之前的回答者已经提供了正确的答案。我想链接到原始的Twitter API文档页面,这样你就知道这实际上是一种官方的做法:
您需要指定?size=
http://api.twitter.com/1/users/profile_image/twitter.json?size=bigger
http://api.twitter.com/1/users/profile_image/twitter.json?size=normal
http://dev.twitter.com/doc/get/users/profile_image/:screen_name
我知道这不是完整的代码示例(因为有几种方法可以做到这一点),但您是否已经拥有头像的URL?我注意到将“.../eric.png”变成“.../eric_bigger.png”会得到更大的图像。当“_bigger”已经存在时,删除它会给我原始图像的URL。
我使用多个关注者的个人资料图像进行了测试,并且在个人资料图像大于150像素正方形时有效。