首页 > 解决方案 > RSS 不会在 PHP 中解析(尝试过 file_get_contents、curl 和 simplexml_load_file)

问题描述

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

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

当然,这适用于任何其他 url...但是这个,虽然它在浏览器中加载得很好,但它为file_get_contentssimplexml_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);

标签: phprsssimplexmlfile-get-contentsphp-curl

解决方案


<?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);

输出是url的内容


推荐阅读