CURL与Facebook图表的通信无法正常工作

时间:2011-10-31 18:44:03

标签: facebook curl facebook-opengraph

大家。

我有以下问题:

我正在使用curl从facebook的图表中获取一些信息(这在几天前就开始工作了),但现在我得到一个空洞的答案。 请求非常简单:

https://graph.facebook.com/?ids=XXX&access_token=YYY

ids参数只是图表中元素的ID列表(在本例中为应用程序请求)。当我在浏览器上复制/粘贴网址时,它可以工作,但是当使用curl时,它会被卡住而没有答案。

curl调用的完整代码是:

require 'php/facebook.php';


    $facebook = new Facebook(array(
            'appId'  => 'XXX',
            'secret' => 'YYY',
    ));


    $url = "https://graph.facebook.com?ids=".$_POST['data']."&access_token=".$_POST['access_token'];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    $results = curl_exec($ch);
    curl_close($ch);
    echo $results;

有人可以对此有所了解吗?

干杯!

1 个答案:

答案 0 :(得分:1)

尝试{} graph.facebook.com/并使用尾部斜杠

 $url = "https://graph.facebook.com/?ids=".$_POST['data']."&access_token=".$_POST['access_token'];

我也不确定你在哪里尝试检索帖子,所以你可以试试请求方法。

 $url = "https://graph.facebook.com/?ids=".$_REQUEST['data']."&access_token=".$_REQUEST['access_token'];

示例ajax调用php:

// get albums

function showAlbums(pageid,limit,offset){
thealbums = "albums";
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    document.getElementById("albums").innerHTML=xmlhttp.responseText;
    }
  };
  xmlhttp.open("GET","plugins.albums.php?pageid="+pageid+"&limit="+limit+"&offset="+offset+"",true);
  xmlhttp.send();
}

尝试cURL:


function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com?ids=".$_POST['data']."&access_token=".$_POST['access_token']");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
$ThisId = GetCH();
echo $ThisId;