3

In PHP, I am trying to post a status to our Facebook fan page using the graph api, despite following the intructions facebook give, the following code does not seem to update the status.

Here is the code;

$xPost['access_token'] = "{key}";
$xPost['message'] = "Posting a message test.";

$ch = curl_init('https://graph.facebook.com/{page_id}/feed'); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xPost); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
curl_setopt($ch, CURLOPT_CAINFO, NULL); 
curl_setopt($ch, CURLOPT_CAPATH, NULL); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

$result = curl_exec($ch); 

Does anyone know why this code is not working? The access_token is correct.

Simon R
  • 3,732
  • 4
  • 31
  • 39

3 Answers3

3
    $url = "https://graph.facebook.com/ID_HERE/feed";
    $ch = curl_init();
    $attachment =  array(   'access_token'  => 'your token',                        
                        'name'          => "Title",
                        'link'          => "www.google.com",
                        'description'   => 'description here',
                    );

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    $result= curl_exec($ch);

    curl_close ($ch);
Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
  • 1
    just another quick question. above command returns some value such as "{"id":"1347466624_1603672123512"}". Is there any way to disable this output? – ericbae Sep 20 '10 at 04:46
  • 1
    I know this was posted back in 2010, but in case someone steps on this question with the same one, the [answer is here](http://stackoverflow.com/a/1918394/1337431) – Diogo Raminhos Apr 27 '12 at 10:49
0

it seems "CURLOPT_SSL_VERIFYPEER" should be set to 0;

e.g. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Simon R
  • 3,732
  • 4
  • 31
  • 39
0

To post photo on Wall photo's album, you need to know the album id (aid) of this album and add it to the attachment like this I leave my code:

    $url = "https://graph.facebook.com/" . $this->getPageId() . "/photos";
    $attachment = array(
        'access_token' => $this->getAccessToken(),
        'source' => '@' . $source,
        'aid' => $aid,
        'message' => $message,
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    ob_start();
    curl_exec($ch);
    $this->setjsonResult(ob_get_contents());
    ob_end_clean();
    curl_close($ch);
Diego125k
  • 241
  • 2
  • 5