【问题标题】:Get last available URL获取最后一个可用的 URL
【发布时间】:2014-11-29 23:28:16
【问题描述】:

我正在使用 API 来获取用户的个人资料图片。调用看起来像这样

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

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

我已经尝试过 Google,但我真的不知道从哪里开始。

提前致谢!

【问题讨论】:

  • @FrisoKluitenberg 这不会将图像下载到用户的本地计算机吗?我只想将直接 URL 存储在数据库中。
  • 否,文件获取内容检索数据,file_put_content 将其存储在服务器上。您可以轻松地将 file_get_contents 的输出存储到 mysql 字段(二进制 blob)中
  • @FrisoKluitenberg 当我这样做并回显或打印它时,我得到一堆符号。我需要直接网址。

标签: php image api url


【解决方案1】:

我能够解决我自己的问题。它正在执行重定向以获取图像,而我只需要该 URL。这是帮助我到达那里的代码。

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2020-01-23
    • 2011-05-31
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多