通过php中的curl连接Fb

时间:2013-03-05 09:35:14

标签: php facebook-graph-api curl

我需要使用curl从Facebook获取用户提要,我已通过curl发送请求并获得

Facebook returned HTTP Error Code: 404 error, follow is my code to connect facebook through curl 
<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_ENCODING , 'gzip, deflate');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, "php-facebook-wall (https://github.com/dordotky/php-facebook-wall)");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url = "https://graph.facebook.com/".FACEBOOK_USER_NAME
    ."/".FACEBOOK_USER_OBJECT."?access_token=".FACEBOOK_APP_ID."|".FACEBOOK_APP_SECRET);

// Execute our curl request
$result = curl_exec($curl);

// Check that we didn't encounter any errors while processing
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ((!curl_error($curl)) && ($http_code == 200))
{
    $json = json_decode($result, true);
} else
{
    die("Facebook returned HTTP Error Code: ".$http_code);
}
?>

1 个答案:

答案 0 :(得分:1)

查看Facebook墙上的帖子图片示例。 需要注意的步骤:

  1. 您的服务器启用了SSL。
  2. 访问令牌在一段时间后发生了变化,因此请使用最新的访问令牌。

    function facebook_upload($ fb_oauth_token,$ item_image) {

    $file = $item_image;
    
        $args = array(
            'access_token'=>urlencode($fb_oauth_token)
        );
    
        $args[basename($file)] = '@'.($file);
    
        $ch = curl_init();
        $url = 'https://graph.facebook.com/me/photos';
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        $data = curl_exec($ch);
        //returns the photo id
        $response = json_decode($data,true);
        //print_r(curl_error($ch));
    
        //to check response
        //if($response[id]>0){
            // yes
        //}
        json_decode($data,true);    
    

    }