首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过file_get_contents获取网址的直接链接

问如何通过file_get_contents获取网址的直接链接
EN

Stack Overflow用户
提问于 2020-07-22 20:06:27
回答 1查看 59关注 0票数 0

我有这样的链接,

代码语言:javascript
复制
https://www.example.com/enter_check/=/declared=yes/?url=https%3A%2F%2Fwww.example.com%2Fitem_detail

当您通过浏览器访问时。

它跳转到https://www.example.com/item_detail

然而,我在PHP上使用file_get_contents访问,它不会跳转,甚至当我假装浏览器是用户代理时,仍然不能工作。

代码语言:javascript
复制
    $context = stream_context_create(array('http' => array(
        'method' => 'GET',
        'header' => 'User-Agent: Mozilla/6.4 (compatible; MSIE 4.0; Windows 98; DigExt)',
        )));
    $source = file_get_contents($url,false,$context);

有什么想法吗?这是否可能使file_get_contents以与浏览器相同的方式工作?

EN

回答 1

Stack Overflow用户

发布于 2020-07-22 20:16:15

我想你需要在你的上下文中添加follow_location选项:

代码语言:javascript
复制
$context = stream_context_create(array('http' => array(
        'method' => 'GET',
        'header' => 'User-Agent: Mozilla/6.4 (compatible; MSIE 4.0; Windows 98; DigExt)',
        'follow_location' => true,
)));
$source = file_get_contents($url,false,$context);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63033826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档