RSS不会在PHP中解析(尝试过file_get_contents,curl和simplexml_load_file)[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我现在完全迷失了,这是URL示例:

file_get_contents('http://adam-wennick.squarespace.com/actor-bro-show?format=rss');

当然这适用于任何其他网址...但是这个,虽然它在浏览器中加载得很好,但是对于file_get_contents和simplexml_load_file都返回400,而对于curl它返回200,但是对象是NULL。你们有没有遇到过这样的事情吗?

卷曲代码:

$rss = 'http://adam-wennick.squarespace.com/actor-bro-show?format=rss'; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, $rss); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
$output = curl_exec($ch);
php rss simplexml file-get-contents php-curl
2个回答
1
投票
<?php

$ch = curl_init("http://adam-wennick.squarespace.com/actor-bro-show?format=rss");

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

print_r($result);

curl_close($ch);

输出是网址的内容


0
投票

如果其他人偶然发现 - 正如@aynber所提到的那样,这个URL正在使用某种保护措施,即使它是RSS应该被刮掉。 :)来吧Squarespace吧!

正如@MagnusEriksson建议的那样,我将file_get_contents与流上下文一起使用,然后将xml_load_file替换为xml_load_string:

$rss = 'http://adam-wennick.squarespace.com/actor-bro-show?format=rss';

$opts = array(
    'http'=> array(
        'method'=>   "GET",
        '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'
      )
);

$context = stream_context_create($opts);
$result = file_get_contents($rss, NULL, $context);
$output = simplexml_load_string($result);

这就是诀窍,$ output现在有了XML对象。再次感谢所有回复如此之快的人。

© www.soinside.com 2019 - 2024. All rights reserved.