如何使用 Twitter API 1.1 获取用户图像?

2021-12-25 00:00:00 twitter php

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

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

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

请问您有解决方案吗?

解决方案

用户头像

好的,所以您需要用户的个人资料图片.您将需要查看 旨在帮助大家轻松或毫不费力地向 1.1 API 发出经过身份验证的请求.

当您使用它时,您会得到上面看到的响应.按照帖子说明,一步一步,您可以在此处获取库(您只需在项目中包含一个文件).

基本上,上一篇文章解释了您需要执行以下操作:

  • 创建一个 Twitter 开发者帐户
  • 从 Twitter 获取一组独特的密钥(总共 4 个密钥).
  • 将您的应用设置为具有读/写访问权限
  • 包括 TwitterApiExchange.php(库)
  • 将您的密钥放入 $settings 数组
  • 从文档中选择您的 URL 和请求方法(发布/获取)(我把链接放在上面!)
  • 提出请求,就是这样!

一个实际例子

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

//需要库文件,很明显require_once('TwitterAPIExchange.php');//使用您从开发站点获得的密钥设置您的设置$设置=数组('oauth_access_token' =>"YOUR_ACCESS_TOKEN",'oauth_access_token_secret' =>"YOUR_ACCESS_TOKEN_SECRET",'consumer_key' =>"YOUR_CONSUMER_KEY",'consumer_secret' =>YOUR_CONSUMER_SECRET");//从文档中选择你想要的 url,这是 users/show$url = 'https://api.twitter.com/1.1/users/show.json';//根据文档,请求方法是 GET,而不是 POST$requestMethod = 'GET';//设置你的获取字符串,我们在这里使用我的网名$getfield = '?screen_name=j7mbo';//创建对象$twitter = new TwitterAPIExchange($settings);//发出请求并将响应放入 $json 变量中$json = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();//是json,所以解码成数组$result = json_decode($json);//访问数组中的 profile_image_url 元素回声 $result->profile_image_url;

差不多就是这样!很简单的.还有 users/lookup 可以有效地做同样的事情,但你可以:

<块引用>

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

如果您需要获取多个用户的详细信息,请使用它,但由于您只需要一个用户的详细信息,请使用如上所示的 users/show.

我希望能把事情弄清楚一点!

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

For example : http://api.twitter.com/1/users/profile_image/EA_FIFA_FRANCE

But, it doesn't work anymore in API 1.1.

Do you have a solution, please ?

解决方案

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:

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:

This is the profile_image_url key that you need to get access to.

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.

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:

  • 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!

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/lookup which 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.

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!

相关文章