Jothi Kannan
Jothi Kannan

Reputation: 3358

Connect Fb through curl in php

i need to get user feeds from Facebook using curl , i have send request through curl and got

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);
}
?>

Upvotes: 0

Views: 1803

Answers (1)

Tony Stark
Tony Stark

Reputation: 8084

Checkout this example of post image on Facebook wall. Steps to take care:

  1. SSL enabled on your server.
  2. Access Token changed after some times so use latest access token.

    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);    
    

    }

Upvotes: 1

Related Questions