提取Twitter个人资料图片

20

有没有一种快速的方法在 PHP 或 JavaScript 中获取 Twitter 账户的头像?我需要获取完整图片的 URL(而不是头像大小)。谢谢。任何代码示例都可以。

8个回答

47

看起来 Twitter 在 v1.1 中删除了该方法的支持。我已经更新了答案,提供了当前推荐的做法。 - abraham
看起来简单的URL又可以用了,例如:**https://api.twitter.com/1/users/profile_image/gb96**。 - gb96
1
@gb96 那是 API v1,将在一个月内停止工作。API v1.1 不支持该 API 端点。 - abraham
1
请参阅 https://dev.twitter.com/docs/user-profile-images-and-banners 和 https://dev.twitter.com/docs/api/1.1/get/users/show 以获取有关 v1.1 的信息。 - Tom
Twitter REST API v1已不再使用,请迁移到API v1.1。 https://dev.twitter.com/docs/api/1.1/overview - Damian

13
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


1
这个函数不再起作用了。还有其他的想法吗?还有其他的解决方法可用吗? - lomse
无法工作。错误:{"errors":[{"message":"抱歉,该页面不存在","code":34}]} - Svetoslav Marinov

6

如果这已经是众所周知的事情,那我很抱歉,但在我的搜索中,包括官方的Twitter文档,都没有找到相关记录。

你可以添加参数?size=original,这将返回用户上传的原始图像。

因此,以下链接将返回原始图像: http://api.twitter.com/1/users/profile_image/twitter.json?size=original


5

之前的回答者已经提供了正确的答案。我想链接到原始的Twitter API文档页面,这样你就知道这实际上是一种官方的做法:

您需要指定?size=

  • bigger - 73px x 73px
  • normal - 48px x 48px
  • mini - 24px x 24px
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


2
因此,虽然在文档中没有提到(http://dev.twitter.com/doc/get/users/profile_image/:screen_name),但是看起来在通过指定三种尺寸之一(bigger、normal、mini)检索图像之后,您可以只需删除文件扩展名前缀即可获得原始图像。嗯...这样使用安全吗?
例如,此查询:api.twitter.com/1/users/profile_image/rrbrambley
结果为:a2.twimg.com/profile_images/931772958/deformed_cropped_headonly_normal.jpg
如果我更改此URL并删除“_normal”,那么我将获得原始图像:a2.twimg.com/profile_images/931772958/deformed_cropped_headonly.jpg
我知道有一些应用程序使用原始图像。这一定是正确的方法吧?

1

0

我知道这不是完整的代码示例(因为有几种方法可以做到这一点),但您是否已经拥有头像的URL?我注意到将“.../eric.png”变成“.../eric_bigger.png”会得到更大的图像。当“_bigger”已经存在时,删除它会给我原始图像的URL。

我使用多个关注者的个人资料图像进行了测试,并且在个人资料图像大于150像素正方形时有效。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接