Ahalya Hegde
Ahalya Hegde

Reputation: 1641

PHP cURL : Post to Facebook Page as Page

I tried a sample code to post to Facebook page. But it is posting as myself. I have included Page_access_token.

permission granted is 'manage_pages' and 'publish_action'. Here is my code :

<?php

    $page_id='xxxx';
    $page_access_token='cccc';
    $url="https://graph.facebook.com/{$page_id}/feed?message=Hello&access_token=".$page_access_token;
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_REFERER, '');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $value = json_decode(curl_exec($ch));
    $var_dump($value);

?>

Where am I going wrong ? How do I solve it ? I want it to be posted as page. Thanks

EDIT: My new code with /$PAGE_ID?fields=access_token:

<?php
  $page_id='xxx';
  $message='helloworld';
  $url="https://graph.facebook.com/v2.3/{$page_id}?fields=access_token";
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_REFERER, '');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $json = json_decode(curl_exec($curl));
  var_dump($json);
?>

This returns an error :

object(stdClass)#1 (1) {
  ["error"]=>
  object(stdClass)#2 (3) {
    ["message"]=>
    string(64) "(#210) A page access token is required to request this resource."
    ["type"]=>
    string(14) "OAuthException"
    ["code"]=>
    int(210)
  }
}

Where am I going wrong ?

Do I need to create the page_access_token from graph API explorer? or the above url is enough? How I am supposed to use User_access_token to get page_access_token ?

Upvotes: 3

Views: 4109

Answers (2)

Gustavo Programmer
Gustavo Programmer

Reputation: 11

    <?php
  $page_id='xxx';
  $message='helloworld';
$access_token = "XXXXXXXXXXXXXXXXXXXXX";
  $url="https://graph.facebook.com/v2.3/{$page_id}/feed/?access_token=".access_token ."&message=".urlencode(message);
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_REFERER, '');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $json = json_decode(curl_exec($curl));
  var_dump($json);
?>

Upvotes: 1

andyrandy
andyrandy

Reputation: 73984

If it gets posted as yourself, you are not using a Page Token. Debug your Token and see if the Page ID is listed - that´s how you know it´s a Page Token: https://developers.facebook.com/tools/debug/

Btw, you should use the new publish_pages permission to post to Pages: https://developers.facebook.com/docs/apps/changelog#v2_3_changes

Information about how to get Access Tokens:

Upvotes: 2

Related Questions