如何使用Twitter API 1.1获取用户图像? [英] How to get user image with Twitter API 1.1?

查看:147
本文介绍了如何使用Twitter API 1.1获取用户图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在API 1.0中,我们可以使用users/profile_image/:screen_name

In API 1.0, we can use users/profile_image/:screen_name

例如:http://api.twitter.com/1/users/profile_image/EA_FIFA_FRANCE

但是,它在 API 1.1中不再起作用.

请问您有解决方案吗?

Do you have a solution, please ?

推荐答案

用户的个人资料图片

好的,因此您需要一个用户的个人资料图像.您需要查看 twitter REST API 1.1文档.这是您可以对其API提出的所有不同请求的列表(不用担心,稍后我会如何您将实际执行此操作).

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 how you actually do this later on).

有多种获取用户个人资料图像的方法,但最值得注意的是:

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:

返回有关由必需的user_id或screen_name参数指定的用户的各种信息.作者的最新Tweet将在可能的情况下以内联方式返回.

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.

好吧,用户个人资料图片必须在某处,对吗?

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).

我已经从底部开始进行了一些删节,因为有很多数据需要处理.最重要的是,您将看到所需的内容:

I've cut off some from the bottom, because there is a lot of data to go through. Most importantly, you'll see what you require:

这是您需要访问的 profile_image_url 键.

那么,您如何做所有这一切?实际上,这非常简单.

So, how do you do all this? It's pretty simple, actually.

正如您正确指出的那样,自2013年6月11日起,您将无法再提出未经身份验证的请求,也不能再向1.0 API发出任何请求,因为该请求已被淘汰.因此,OAuth是向1.1 API发出请求的方式.

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.

我写了

I wrote a stack overflow post with an aim to help all you guys make authenticated requests to the 1.1 API with little to no effort.

使用它时,您将获得上面看到的响应.逐步按照帖子中的说明进行操作,您可以在此处 (您只需在项目中包含一个文件即可.)

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:

  • 创建一个Twitter开发者帐户
  • 从Twitter获得一组独特的密钥(总共4个密钥).
  • 将您的应用程序设置为具有读/写访问权限
  • 包括TwitterApiExchange.php(该库)
  • 将密钥放入$settings数组
  • 从文档中选择您的URL和请求方法(发布/获取)(我将链接放在上面!)
  • 发出请求,就是这样!
  • 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 $settings array
  • Choose your URL and request method (Post/Get) from the docs (I put the link above!)
  • Make the request, that's it!

我将假设您按照上述文章中的分步说明进行操作(包含漂亮的彩色图片).这是您用来获取所需内容的代码.

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;

差不多就可以了!很简单.还有 users/lookup 可以有效地完成相同的操作,但您可以:

That's pretty much it! Very simple. There's also users/lookup which effectively does the same thing, but you can:

根据传递给user_id和/或screen_name参数的逗号分隔值的指定,每个请求最多为100个用户返回完全水合的用户对象.

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.

如果您需要获得多个用户的详细信息,请使用该信息,但是由于您仅需要一个用户的详细信息,请使用上述用户/显示.

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.

我希望能使事情变得简单起来!

I hope that cleared things up a bit!

这篇关于如何使用Twitter API 1.1获取用户图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆