Php 从其他域加载网站

Php 从其他域加载网站,php,Php,在我的应用程序中,我从供应商处加载产品信息: $start_url = "http://www.example.com/product/product_code"; 这些URL通常由供应商的网站重定向,我编写了一个函数,可以成功地找到目标URL,如下所示: $end_url = destination( $start_url ); echo "<a href=\"$start_url\">start url</a>"; // link get redirected to

在我的应用程序中,我从供应商处加载产品信息:

$start_url = "http://www.example.com/product/product_code";
这些URL通常由供应商的网站重定向,我编写了一个函数,可以成功地找到目标URL,如下所示:

$end_url = destination( $start_url );
echo "<a href=\"$start_url\">start url</a>"; // link get redirected to correct page
echo "<a href=\"$end_url\">end url</a>"; // links straight to correct page, no redirection
…我刚得到供应商的404页面(不是一般页面,而是他们的自定义页面)

我已启用
允许url\u fopen
<代码>文件获取内容(“http://www.example.com/)工作正常

我可以使用任意一个URL在
iframe
客户端加载预期内容,但XSS安全性阻止我提取所需的数据

我唯一能想到的是,如果网站使用URL重写器,这会把事情搞砸吗


PHP是在我的本地机器上运行的,所以据我所知,它应该与我通过浏览器查看网站时没有什么不同。

多亏了@Loz Cheroneツ's注释,使用cURL和worked


然后我将响应放入
iframe
客户端的
srcdoc
属性中,这样我就可以访问DOM了。

Ok,那么
destination()做什么呢?你的问题很可能就在那里。或者该站点正在抛出一个404 for bots,并将
php
作为用户代理。本质上,它测试
位置:
重定向的响应,然后测试新url,直到不再接收重定向并返回最终url。当使用ajax请求时,它生成的URL在地址栏/iframe src中工作。我没有想到用户代理——我会调查的。你可能想研究一下使用curl,你可以设置用户代理、会话或cookies,并通过几行代码遵循重定向CURLOPT_FOLLOWLOCATION(只要没有有效的基本目录限制),加上curl比std fgc快50%
echo file_get_contents( $start_url );  // 404
echo file_get_contents( $end_url );  // 404
$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13";

$url = $_REQUEST["url"];  // e.g. www.example.com/product/ABC123            

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  // follows any redirection
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

echo curl_exec($ch);

curl_close($ch);