26

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 ?

Jimbo
  • 25,790
  • 15
  • 86
  • 131
Steffi
  • 6,835
  • 25
  • 78
  • 123

7 Answers7

51

You can also get the twitter profile image by calling this kind of url :

https://twitter.com/[screen_name]/profile_image?size=original

For instance : 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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Simon Briche
  • 1,304
  • 11
  • 6
34

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

Example response for users/show from the twitter 1.1 api

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 key

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!

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • so there is no (known) unauthenticated hack/bypass that visits their page and gets the link then... sigh. – Isaiah Turner Jun 14 '13 at 17:35
  • 1
    @IsaiahTurner No hacks, and if there were I'm sure you would tell them so they could fix the exploit asap ;) I have tried to make it as easy as possible for you, though. – Jimbo Jun 14 '13 at 17:38
  • Haha, no I want a hack not for them to patch it. I hate this part of API 1.1. If nobody comes up with a better solution, and that seems to be where this is heading, I'll give you the rep. The reason I wanted this was to have a contact page on my site with profile images that automatically updated to match our Twitter. – Isaiah Turner Jun 14 '13 at 17:46
  • 1
    I see, well if you want a hack, can't you just scrape your actual twitter page using file_get_contents() then parse the HTML for the user profile image? – Jimbo Jun 14 '13 at 17:48
  • I will try that, not very good with web development but at least I know what to google now. And for lack of a better answer, enjoy 50 rep. – Isaiah Turner Jun 15 '13 at 20:54
  • +1 Thank you so much for both posts and your library @Jimbo ! They were very helpful to me for getting my application working. – Anthony Jul 29 '13 at 16:33
  • Could the `profile_image_url` be cached? That is, if I want the user's Twitter profile picture the show up on my app client, should the app client make the `GET` request or should the server backend do it and pass the image url to the app? – Kar Feb 13 '14 at 10:51
  • @Kate Caching implementation is entirely up to you - sounds like a good idea to grab the image and cache it either server-side or on your app so it doesn't have to be constantly downloaded - same goes for bio info and other data that wont change often :-) – Jimbo Feb 13 '14 at 10:53
  • @Jimbo I mean, could the URL itself be cached. That is, the server returns the same URL to the client. If the URL is very short-lived or expires after an access, then that wouldn't be feasible. – Kar Feb 13 '14 at 10:55
  • @Kate Hmm, I see what you're saying.. I don't know if it expires to be honest. I wouldn't go about caching the URL to avoid having to make requests to get the url though - because you're still going to have to make another request to actually download the image. I would be downloading the image and caching it once, for maybe 24 hours (you could base64 encode / decode it to pass it around). – Jimbo Feb 13 '14 at 11:05
2

I try the above methods to get the profile URL but it does not work for me. I think because Twitter changes API v1.1 to API v2.0.

I found a simple method to get a profile URL.

I use Twitter API v2 there User Lookup -> User by Username API part

Code Sample:

https://api.twitter.com/2/users/by/username/{user_name}?user.fields=profile_image_url

For Example:

https://api.twitter.com/2/users/by/username/TwitterDev?user.fields=profile_image_url

Of course, You should request with your Bearer Token then it properly work. For that, I recommend a platform it calls postman. It really helps for calling API.

Above example code return JSON like this:

{
"data": {
    "name": "Twitter Dev",
    "profile_image_url": "https://pbs.twimg.com/profile_images/1445764922474827784/W2zEPN7U_normal.jpg",
    "username": "TwitterDev",
    "id": "2244994945"
}

}

Additional:

If You want the Profile Image to be a higher size. Then you can put size in place of normal in the URL. For More Details read this one

Like This:

https://pbs.twimg.com/profile_images/1445764922474827784/W2zEPN7U_400x400.jpg

Give a vote to help more developers.

Prince Patel
  • 319
  • 3
  • 5
1

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 :

You can get image from profile_image_url field of https://api.twitter.com/1.1/users/show.json request. Either a id or screen_name is required for this method. For example :

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

user568109
  • 47,225
  • 17
  • 99
  • 123
0

As the previous answers and comments point out:

  1. Twitter API v1.0 is deprecated
  2. Twitter API v1.1 requires OAuth
  3. OP (@Steffi) doesn't want to authenticate

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:

curl -s https://twitter.com/EA_FIFA_FRANCE |
  sed -ne 's/^.*ProfileAvatar-image.*\(https:[^"]*\).*$/\1/p'

The sed command just says, find the line that contains "ProfileAvatar-image" and print the substring that looks like a quoted 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!

The PHP translation should be straightforward.

chbrown
  • 11,865
  • 2
  • 52
  • 60
-1

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

https://dev.twitter.com/docs/api/1.1/get/users/show

and retrieve after his picture

profile_image_url
Osin
  • 485
  • 1
  • 4
  • 12
  • 1
    check here [link]http://stackoverflow.com/questions/12560753/twitter-profile-image-api-deprecated?rq=1[link] – Osin Feb 12 '13 at 16:33
  • 1
    Yeah, but I don't want to connect through my app. I prefer when "No authentication is required" – Steffi Feb 12 '13 at 16:33
  • 1
    @Steffi With the new API your going to have to connect to oAuth, for a more detailed response: http://stackoverflow.com/questions/17049821/setting-up-twitter-api-getting-the-last-few-tweets/17057693#17057693 – Starboy Jun 12 '13 at 18:16
-2

Hare is a very simple way to get Twitter Profile picture.

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

Small: 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.

Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37