无法使用CURL获取Feed内容

时间:2016-03-29 15:30:35

标签: php curl

我试图获取此Feed的内容: http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss

这是我的代码:

$url = 'http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss';

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER         => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING       => "",
CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
CURLOPT_AUTOREFERER    => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT        => 30,
CURLOPT_MAXREDIRS      => 10
);

$curl = curl_init($url); 
curl_setopt_array( $curl, $options ); 
$content = curl_exec($curl); 
curl_close($curl);

echo $content;

我尝试了很多其他CURL选项,但它没有用。 由于可以通过我的浏览器访问内容,我想可以使用PHP完成。但是我的代码有什么问题?看来这个Feed的服务器有例外吗?

1 个答案:

答案 0 :(得分:0)

不确定,可能是您打破了cURL选项并调用了URL。 这是一个简单的例子,试一试:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$content = get_data('http://www.institut-viavoice.com/viavoice-paris/publications/sondages-publies?format=feed&type=rss');

echo $content;