获取最后一个可用的URL

问题描述:

我正在使用API​​来获取用户个人资料图片。看起来像这样的电话获取最后一个可用的URL

https://familysearch.org/platform/tree/persons/{$rid}/portrait?access_token={$_SESSION['fs-session']}&default=https://eternalreminder.com/dev/graphics/{$default_gender}_default.svg 

此链接仅适用于大约一个小时,因为用户的会话令牌到期了。我想知道是否有任何方法来检索最后返回的返回的URL,这将是图像的直接链接,所以我可以将它存储在数据库中。

我试过谷歌,但我不知道从哪里开始。

提前致谢!

+0

见http://*.com/questions/724391/saving-image-from-php-url – 2014-10-05 15:07:46

+0

@FrisoKluitenberg这会不会将图像下载到用户本地计算机?我只想将直接URL存储在数据库中。 – blazerunner44 2014-10-05 15:11:15

+0

不,文件获取内容检索数据,file_put_content将其存储在服务器上。你可以很容易地将file_get_contents的输出存储到一个mysql字段(二进制blob) – 2014-10-05 15:15:19

我能解决我自己的问题。这是做重定向来获取图像,我只需要该网址。这是我的代码,帮助我到达那里。

$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$a = curl_exec($ch); // $a will contain all headers 

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL 

// Uncomment to see all headers 
/* 
echo "<pre>"; 
print_r($a);echo"<br>"; 
echo "</pre>"; 
*/ 

echo $url; // Voila