【发布时间】:2013-08-25 06:12:09
【问题描述】:
有什么方法可以使用用户 ID 或屏幕名称构建个人资料图片网址吗?我将用户 ID 存储在数据库中,但我不想存储个人资料图片网址。
编辑:
我也不想进行 api 调用。我想把 user_id 放在像
<img src="https://twitter.com/users/profile_pic?user_id=123"> 有网址吗?
【问题讨论】:
标签: twitter
有什么方法可以使用用户 ID 或屏幕名称构建个人资料图片网址吗?我将用户 ID 存储在数据库中,但我不想存储个人资料图片网址。
编辑:
我也不想进行 api 调用。我想把 user_id 放在像
<img src="https://twitter.com/users/profile_pic?user_id=123"> 有网址吗?
【问题讨论】:
标签: twitter
使用 API 1.1,您可以使用以下 URL 实现此目的:
Official twitter documentation Profile Images and Banners
示例
https://twitter.com/TwitterEng/profile_image?size=original
将重定向到
https://pbs.twimg.com/profile_images/875168599299637248/84CkAq6s.jpg
【讨论】:
https://twitter.com/Rashid_Jorvee/profile_image?size=original,但是显示错误Sorry, that page doesn’t exist!
介绍在不使用 Twitter API 的情况下获取 Twitter 个人资料图片的最简单方法:
正如@AlexB,@jfred 所说,它在移动设备上根本不起作用。
在您的单个页面中使用 PHP 或 JavaScript 等通用框架获取重定向 URL 是一种非常困难的方法。
只需在您的图片标签处调用http://avatars.io/twitter/ruucm,例如
<img src="https://avatars.io/twitter/ruucm" alt="twt_profile" border="0" width="259"/>
我已经用 Angular 2+ 对其进行了测试,它没有任何问题。
【讨论】:
自 2020 年 6 月起,接受的答案和 avatars.io 都不再有效。这里有两个替代方案:
(以前的 unavatar.now.sh)
Unavatar 可以从很多不同的地方获取图片,包括 Twitter。将以下 URL 中的 [screen_name] 替换为您想要的 Twitter 用户名。
<img src="https://unavatar.io/twitter/[screen_name]" />
例如:
<img src="https://unavatar.io/twitter/jack" width="100" height"100" />
如果上面的演示停止工作,可能是因为 unavatar.io 不再可用。
不过,Unavatar 是开源的,所以如果它出现故障,您可以从 the GitHub repo 自行部署它——它甚至有“部署到 Vercel/Heroku”按钮。获取 Twitter 头像的代码是 here,因此您也可以将其用作您自己后端的一部分。
⚠️ 自 2021 年 7 月起,此选项不再有效,请参阅上面的选项!
如果你想要一个替代方案,你也可以使用twivatar.glitch.me。将下面 URL 中的 [screen_name] 替换为您想要的 Twitter 用户名。
<img src="https://twivatar.glitch.me/[screen_name]" />
例如:
<img src="https://twivatar.glitch.me/jack" width="100" height"100" />
如果上面的演示停止工作,可能是因为 twivatar.glitch.me 不再可用。
顺便说一句,我没有构建这些服务,它们都是由其他人制作的。
【讨论】:
您可以使用users/show method of the Twitter API 获取它——它完全按照您的描述进行。你给它一个ID或者网名,它会返回一堆数据,包括profile_image_url。
【讨论】:
<img src="https://twitter.com/users/profile_pic?user_id=123"> 这样的网址中,这可能吗?
根据@Cristiana214 的回答
以下 PHP sn-p 可用于使 https://twitter.com/[screen_name]/profile_image?size=normal 技巧在移动设备上运行。
由于 twitter 重定向到移动版的网站链接,例如 https://twitter.com/[screen_name]/profile_image?size=normal 在移动设备上被破坏
因此脚本获取重定向响应(对用户头像)提取地址然后重定向页面本身
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]);
所以这可以作为 twitteravatar.php?id=twitter 访问,它(在撰写本文时)重新加载到 https://pbs.twimg.com/profile_images/767879603977191425/29zfZY6I_normal.jpg
不漂亮但有效。
【讨论】:
截至 2020 年 2 月 20 日,这似乎是不可能的。使用 API 似乎是目前唯一的选择。有关更多信息,请参阅我在这里打开的问题:Twitter profile picture images now blocked on most domains
【讨论】:
我用 C# 找到了这样的解决方案:
public string Text_toTextFinder(string text, string Fromhere, string Here)
{
int start = text.IndexOf(Fromhere) + Fromhere.Length;
int finish = text.IndexOf(Here, start);
return text.Substring(start, finish - start);
}
string getPhotoURL(string UserName, string size ="x96")
{
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
string htmlCode = client.DownloadString("https://twitter.com/" + UserName);
return Text_toTextFinder(Text_toTextFinder(htmlCode, "<td class=\"avatar\">", "</td>"), "src=\"", "\"").Replace("normal",size);
}
}
使用:
MessageBox.Show(getPhotoURL("screen_name")); //size = 96x96
MessageBox.Show(getPhotoURL("screen_name","normal"));
MessageBox.Show(getPhotoURL("screen_name","200x200"));
MessageBox.Show(getPhotoURL("screen_name","400x400"));
【讨论】:
没有办法做到这一点。事实上,Twitter 并没有像 facebook 那样提供一个 url (https://graph.facebook.com//?fields=picture)
问题是报告,但状态是:'WontFix',看看:
【讨论】:
好吧,我通过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
这会给你这样的东西; https://pbs.twimg.com/profile_images/378800000487958092/e04a191de329fcf8d000ca03073ad594_bigger.png
获得 2 个其他尺寸;对于大版本删除,“_bigger”对于较小版本将“_bigger”替换为“_normal”
【讨论】:
对于 1.1 版,使用 http://a0.twimg.com/profile_images/XXXXX/afpecvf41m8f0juql78p_normal.png 其中 XXXXX 是用户 ID
【讨论】:
_normal.png 之前的第二个哈希也是唯一的...这不起作用。