php 如何使用 Twitter API 1.1 获取用户图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14836956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get user image with Twitter API 1.1?
提问by Steffi
In API 1.0, we can use users/profile_image/:screen_name
在 API 1.0 中,我们可以使用 users/profile_image/:screen_name
For example : http://api.twitter.com/1/users/profile_image/EA_FIFA_FRANCE
例如 : http://api.twitter.com/1/users/profile_image/EA_FIFA_FRANCE
But, it doesn't work anymore in API 1.1.
但是,它在API 1.1中不再起作用。
Do you have a solution, please ?
请问你有解决办法吗?
回答by Simon Briche
You can also get the twitter profile image by calling this kind of url :
您还可以通过调用这种 url 来获取 Twitter 个人资料图片:
https://twitter.com/[screen_name]/profile_image?size=original
https://twitter.com/[screen_name]/profile_image?size=original
For instance : https://twitter.com/VancityReynolds/profile_image?size=original
例如:https: //twitter.com/VancityReynolds/profile_image?size=original
Got the info from this post :
从这篇文章中得到了信息:
https://twittercommunity.com/t/how-to-get-user-image-original-size-with-api-1-1/10187/14
https://twittercommunity.com/t/how-to-get-user-image-original-size-with-api-1-1/10187/14
回答by Jimbo
The user's profile image
用户的个人资料图片
Okay, so you want a user's profile image. You're going to need to take a look at the twitter REST API 1.1 docs. This is a list of all the different requests you can make to their API (don't worry, I'll get to howyou actually do this later on).
好的,所以您需要用户的个人资料图片。您将需要查看twitter REST API 1.1 文档。这是您可以向他们的 API 发出的所有不同请求的列表(别担心,我稍后会介绍您实际如何执行此操作)。
There are multiple ways to get the user's profile image, but the most notable one is: users/show. According to the docs for this, the users/show method:
有多种方法可以获取用户的个人资料图片,但最值得注意的一种是:users/show。根据文档,users/show 方法:
Returns a variety of information about the user specified by the required user_id or screen_name parameter. The author's most recent Tweet will be returned inline when possible.
返回有关由所需的 user_id 或 screen_name 参数指定的用户的各种信息。如果可能,作者的最新推文将被内联返回。
Well, the user profile image must be in there somewhere, correct?
好吧,用户个人资料图片必须在某处,对吗?
Let's have a look at a typical response to a request for this information, using the users/show url (we'll use my profile as an example).
让我们使用 users/show url(我们将使用我的个人资料作为示例)来看看对此信息请求的典型响应。


I've cut off some from the bottom, because there is a lotof data to go through. Most importantly, you'll see what you require:
我从底部剪掉了一些,因为有很多数据要处理。最重要的是,你会看到你需要什么:


This is the profile_image_urlkey that you need to get access to.
这是您需要访问的profile_image_url密钥。
So, how do you do all this? It's pretty simple, actually.
那么,你如何做到这一切?其实很简单。
Authenticated Requests
经过身份验证的请求
As you rightly pointed out, as of June 11th 2013 you can't make unauthenticated requests, or any to the 1.0 API any more, because it has been retired. So OAuth is the way to make requests to the 1.1 API.
正如您正确指出的那样,截至 2013 年 6 月 11 日,您不能再向 1.0 API 发出未经身份验证的请求或任何请求,因为它已停用。所以 OAuth 是向 1.1 API 发出请求的方式。
I wrote a stack overflow postwith an aim to help all you guys make authenticated requests to the 1.1 API with little to no effort.
我写了一篇堆栈溢出帖子,目的是帮助你们所有人毫不费力地向 1.1 API 发出经过身份验证的请求。
When you use it, you'll get back the response you see above. Follow the posts instructions, step-by-step, and you can get the library here(you only need to include one file in your project).
当你使用它时,你会得到上面看到的响应。按照帖子说明一步一步来,您可以在此处获取库(您只需在项目中包含一个文件)。
Basically, the previous post explains that you need to do the following:
基本上,上一篇文章解释了您需要执行以下操作:
- Create a twitter developer account
- Get yourself a set of unique keys from twitter (4 keys in total).
- Set your application to have read/write access
- Include TwitterApiExchange.php (the library)
- Put your keys in a
$settingsarray - Choose your URL and request method (Post/Get) from the docs (I put the link above!)
- Make the request, that's it!
- 创建 Twitter 开发者帐户
- 从 Twitter 获取一组唯一的密钥(总共 4 个密钥)。
- 将您的应用程序设置为具有读/写访问权限
- 包括 TwitterApiExchange.php(库)
- 把你的钥匙放在一个
$settings数组中 - 从文档中选择您的 URL 和请求方法(发布/获取)(我把链接放在上面!)
- 提出要求,就是这样!
A practical example
一个实际的例子
I'm going to assume you followed the step-by-step instructions in the above post (containing pretty colour pictures). Here's the code you would use to get what you want.
我将假设您按照上述帖子中的分步说明进行操作(包含漂亮的彩色图片)。这是您用来获得所需内容的代码。
// Require the library file, obviously
require_once('TwitterAPIExchange.php');
// Set up your settings with the keys you get from the dev site
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
// Chooose the url you want from the docs, this is the users/show
$url = 'https://api.twitter.com/1.1/users/show.json';
// The request method, according to the docs, is GET, not POST
$requestMethod = 'GET';
// Set up your get string, we're using my screen name here
$getfield = '?screen_name=j7mbo';
// Create the object
$twitter = new TwitterAPIExchange($settings);
// Make the request and get the response into the $json variable
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
// It's json, so decode it into an array
$result = json_decode($json);
// Access the profile_image_url element in the array
echo $result->profile_image_url;
That's pretty much it! Very simple. There's also users/lookupwhich effectively does the same thing, but you can:
差不多就是这样!很简单。还有用户/查找可以有效地做同样的事情,但你可以:
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters.
根据传递给 user_id 和/或 screen_name 参数的逗号分隔值的指定,为每个请求返回最多 100 个用户的完全水合用户对象。
If you ever need to get more than one user's details, use that, but as you only require one user's details, use users/show as above.
如果您需要获取多个用户的详细信息,请使用它,但由于您只需要一个用户的详细信息,请使用如上所示的 users/show。
I hope that cleared things up a bit!
我希望能把事情弄清楚一点!
回答by user568109
You say you want to use Twitter API 1.1 and yet you don't want to authenticate your requests. Unauthenticated requests are not supported in API v1.1.So please adjust to the API change. See updates :
您说您想使用 Twitter API 1.1,但又不想验证您的请求。 API v1.1 不支持未经身份验证的请求。所以请适应 API 的变化。查看更新:
- https://dev.twitter.com/blog/planning-for-api-v1-retirement
- https://dev.twitter.com/docs/rate-limiting/1.1
- https://dev.twitter.com/blog/planning-for-api-v1-retirement
- https://dev.twitter.com/docs/rate-limiting/1.1
You can get image from profile_image_urlfield of https://api.twitter.com/1.1/users/show.jsonrequest. Either a idor screen_nameis required for this method. For example :
您可以从请求profile_image_url字段中获取图像https://api.twitter.com/1.1/users/show.json。此方法需要aid或screen_name。例如 :
GET https://api.twitter.com/1.1/users/show.json?screen_name=rsarver
See details here https://dev.twitter.com/docs/api/1.1/get/users/show
在此处查看详细信息https://dev.twitter.com/docs/api/1.1/get/users/show
回答by Osin Toumani
try this
尝试这个
http://api.twitter.com/1/users/profile_image/{twitter_account}.xml?size=bigger
In API 1.1 the only way is to connect your application, retrieve the user by
在 API 1.1 中,唯一的方法是连接您的应用程序,通过以下方式检索用户
https://dev.twitter.com/docs/api/1.1/get/users/show
and retrieve after his picture
并在他的照片后检索
profile_image_url
回答by chbrown
As the previous answers and comments point out:
正如之前的答案和评论指出的那样:
- Twitter API v1.0 is deprecated
- Twitter API v1.1 requires OAuth
- OP (@Steffi) doesn't want to authenticate
- Twitter API v1.0 已弃用
- Twitter API v1.1 需要 OAuth
- OP (@Steffi) 不想进行身份验证
Pick any two; with all three it's a no-go. @Jimbo's answer is correct (and the proper way to do it), but excludes #3. Throwing out #1 means going back in time. But, we can throw out #2, and go directly to the source:
选择任意两个;这三个都是不行的。@Jimbo 的答案是正确的(以及正确的方法),但不包括 #3。扔掉#1 意味着回到过去。但是,我们可以扔掉#2,直接去源码:
curl -s https://twitter.com/EA_FIFA_FRANCE |
sed -ne 's/^.*ProfileAvatar-image.*\(https:[^"]*\).*$//p'
The sedcommand just says, find the line that contains "ProfileAvatar-image" and print the substring that looks like a quoted URL.
该sed命令只是说,找到包含“ProfileAvatar-image”的行并打印看起来像带引号的 URL 的子字符串。
This is less stable than an authenticated API call, since Twitter may change their HTML at any time, but it's easier than dealing with OAuth, and no official rate limits!
这不如经过身份验证的 API 调用稳定,因为 Twitter 可能随时更改其 HTML,但它比处理 OAuth 更容易,而且没有官方速率限制!
The PHPtranslation should be straightforward.
在PHP翻译应该是简单的。
回答by Avtandil Kavrelishvili
Hare is a very simple way to get Twitter Profile picture.
Hare 是一种获取 Twitter 个人资料图片的非常简单的方法。
http://res.cloudinary.com/demo/image/twitter_name/w_300/{User_Name}.jpg
http://res.cloudinary.com/demo/image/twitter_name/w_300/{User_Name}.jpg
it's my Profile picutre: Big: http://res.cloudinary.com/demo/image/twitter_name/w_300/avto_key.jpg
这是我的个人资料图片:大:http://res.cloudinary.com/demo/image/twitter_name/w_300/avto_key.jpg
Small: http://res.cloudinary.com/demo/image/twitter_name/w_100/avto_key.jpg
小:http: //res.cloudinary.com/demo/image/twitter_name/w_100/avto_key.jpg
you can regulate size by this part of URL - w_100, w_200, w_500 and etc.
您可以通过 URL 的这一部分来调整大小 - w_100、w_200、w_500 等。

