2014-10-05 74 views
1

我正在使用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://stackoverflow.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

回答

1

我能解決我自己的問題。這是做重定向來獲取圖像,我只需要該網址。這是我的代碼,幫助我到達那裏。

$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