cURL和Facebook问题 [英] cURL and Facebook problems
问题描述
我使用cURL访问Facebook页面。在本地它工作完美,但当我上传到我的dev服务器,它打破并返回一个空字符串。我检查和cURL安装在服务器上。这里是我用来访问facebook的代码:
I am using cURL to access a facebook page. Locally it works perfect, but when I upload it to my dev server, it breaks and returns an empty string. I've checked and cURL is installed on the server. Here's the code I use to access facebook:
$header = array();
$header[] = 'Accept: text/json';
$header[] = 'Cache-Control: max-age=0';
$header[] = 'Connection: keep-alive';
$header[] = 'Keep-Alive: 300';
$header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$header[] = 'Accept-Language: en-us,en;q=0.5';
$header[] = 'Pragma: ';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://facebook.com/feeds/page.php?format=json&id=135137236003');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close ($ch);
感谢任何帮助。
推荐答案
将接受标头更改为 * / *
或 application / json
发送响应头作为 application / json
。
Change the accept header to */*
or application/json
as facebook is sending the response header as application/json
.
并更改此URL
http://facebook.com/feeds/page.php?format=json&id=135137236003
到
http://www.facebook.com/feeds/page.php?format=json&id=135137236003
因为facebook正在重定向非www请求www请求。虽然它适合你,因为put按照位置,但它节省了一个回程旅行
as facebook is redirecting the non-www request to www requests. Though it works for you as put follow location, but it saves one reound trip
这篇关于cURL和Facebook问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!