繁体   English   中英

Curl 请求失败

[英]Curl request fails

我有一个小问题。 我想加载这个页面'http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20'使用ZF6E57C9DE7515E43548ZF9。 这是我尝试使用的代码,但我无法从中获得任何回报。

    $head=array('Accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Charset'=>'ISO-8859-2,utf-8;q=0.7,*;q=0.3',
                    'Accept-Encoding'=>'gzip,deflate,sdch',
                    'Accept-Language'=>'ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Cache-Control'=>'max-age=0',
                    'Connection'=>'keep-alive',
                    'Cookie'=>'datr=hroHTi2NZk2KleOaswb03Q_Q; lu=gg9lJcPeInHt6hnut7bviqQg; locale=en_US; e=n; L=2; c_user=100000596376783; sct=1309129360; xs=2%3Ad05dd80e364608525dd664ad73f6483f; act=1309410851554%2F5; presence=EM309410852L4N0_5dEp_5f1B00596376783F1X309410852168Y0Z11G309410768PCC',
                    'Host'=>'www.facebook.com',
                    'User-Agent'=>$_SERVER['HTTP_USER_AGENT']);  
$url='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20';
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
     $result = curl_exec($ch);

        return $result;

我试过没有设置 header 但它也没有用。 希望有人能给我一个线索。

谢谢!

这是你的问题: $ur='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20'; . 你少了一个“l”。

$url='http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20';

此外,还有一个重定向,因此您需要添加:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

我是这样发现的。 我使用curl_setopt($ch, CURLOPT_VERBOSE, true); 看到了这个:

< HTTP/1.1 302 Found
< Location: http://www.facebook.com/common/browser.php
< Content-Type: text/html; charset=utf-8
< X-FB-Server: 10.43.97.37
< X-Cnection: close
< Date: Thu, 30 Jun 2011 06:48:54 GMT
< Content-Length: 0

更新、测试和工作:

Facebook 需要指定用户代理字符串。 使用curl_setopt($ch, CURLOPT_USERAGENT, '...');设置它似乎可以解决问题。 这是一个过于简单的示例,应该可以解决您的问题:

<?php

$ch = curl_init('http://www.facebook.com/feeds/page.php?id=57084011597&format=rss20');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, array('User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Safari/534.45'));

$response = curl_exec($ch);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM