Building Twitter profile image url with Twitter us

2019-01-17 00:22发布

Is there any way of building a profile image url with user id or screen name? I store user ids in database but i don't want to store profile image url.

edit:
I don't want to make a api call too. I want to put user_id inside a url like

<img src="https://twitter.com/users/profile_pic?user_id=123"> Is there a url to do this?

标签: twitter
7条回答
ら.Afraid
2楼-- · 2019-01-17 01:00

Well I'm using a tricky way via PHP Dom Parser

include('simple_html_dom.php');
$html = file_get_html('http://twitter.com/mnckry');
$img = array();

foreach($html->find('img.size73') as $e)
    $img[] = $e->src;

foreach($html->find('.profile-header-inner') as $e)
    $img[] = str_replace("')", "", str_replace("url('", "", $e->{'data-background-image'}));



echo $img[0];//Avatar
echo "<br>";
echo end($img);//ProfileBG

This will give you something like this; https://pbs.twimg.com/profile_images/378800000487958092/e04a191de329fcf8d000ca03073ad594_bigger.png

to get 2 other size; for big version remove, "_bigger" for smaller version replace "_bigger" with "_normal"

查看更多
SAY GOODBYE
3楼-- · 2019-01-17 01:05

Introducing the easiest way to get Twitter Profile Image without Twitter API.

Using http://avatars.io/

As @AlexB, @jfred says it doesn't work at all on MOBILE devices.

And it's a quite HARD way to get redirected URL using common frameworks PHP or Javascript in your single page.

Simply call http://avatars.io/twitter/ruucm at your image tag.

like

<img src="https://avatars.io/twitter/ruucm" alt="twt_profile" border="0" width="259"/>

I've tested it Angular 2+ and it works but any problem

查看更多
ら.Afraid
4楼-- · 2019-01-17 01:07

Based on the answer by @Cristiana214

The following PHP snippet can be used to make the https://twitter.com/[screen_name]/profile_image?size=normal trick work on mobile.

Due to twitters redirect to the mobile version of the site links such as https://twitter.com/[screen_name]/profile_image?size=normal get broken on mobile devices

So the script gets the redirect response (to the user avatar) extracts the address then redirects the page itself

if (!isset($_GET['id'])) $_GET['id'] = 'twitter';
$urlget = curl_init();
curl_setopt($urlget, CURLOPT_URL, 'https://twitter.com/' . $_GET['id'] . '/profile_image?size=normal'); 
curl_setopt($urlget, CURLOPT_HEADER, true);
curl_setopt($urlget, CURLOPT_RETURNTRANSFER, 1); 
$res = curl_exec($urlget);
preg_match_all("/location: (.*)/", $res, $found);
header('Location: ' . $found[1][0]);

So this could be accesses as twitteravatar.php?id=twitter which (at time of writing) reloads to https://pbs.twimg.com/profile_images/767879603977191425/29zfZY6I_normal.jpg

Not pretty but works.

查看更多
Animai°情兽
5楼-- · 2019-01-17 01:13
SAY GOODBYE
6楼-- · 2019-01-17 01:14

with API 1.1 you can achieved using this url

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

Official twitter documentation Profile Images and Banners

Example

  https://twitter.com/TwitterEng/profile_image?size=original 

will redirect to

    https://pbs.twimg.com/profile_images/875168599299637248/84CkAq6s.jpg
查看更多
再贱就再见
7楼-- · 2019-01-17 01:18

You can get it using the users/show method of the Twitter API -- it does exactly what you described. You give it a the ID or the screen name, and it returns a bunch of data, including profile_image_url.

查看更多
登录 后发表回答