0

I'm developing an application in php using graph api. So, I have to post messages to a Facebook page as admin. I followed this: How to post to Facebookpage as admin via API (Php SDK)? . It worked well a couple of time, but now it returns this error: PHP Fatal error: Uncaught GraphMethodException: Unsupported post request.\n thrown in /home/redmarv/public_html/owl-test/facebook.php on line 453

But can't find where the problem is. Even with google or search other posts right there. But I can't understand why it works a couple of time and then no. Without changing anything.

Actually I use this code:

include_once "fbmain.php";

echo 'Hi ' . $uid . '<br />';

$fb_accounts = $facebook->api('/me/accounts');

$access_token = $fb_accounts['data'][1]['access_token'];

echo 'Access Token= ' . $access_token;

$facebook->api('/page_id/feed', 'post', array('message'=>'OWL F TOOLS N TEST by: '.$uid,  'access_token'=>$access_token, 'cb'=>''));

The included fbmain.php does the session contro and create the $facebook object. Sorry for my bad English and thanks.

Community
  • 1
  • 1
redmarv
  • 117
  • 1
  • 2
  • 7

2 Answers2

2

If a user isn't logged into your app while making graph requests, you'll get the error you describe above.

I'm assuming the code in 'fbmain.php' handles authenticating the user and returning the $uid, but if you're already not logged in before hitting this script, the code after the include 'fbmain.php' is probably running before Facebook does the login redirect.

Try including a check to make sure $uid actually contains a valid integer before using it.

include_once "fbmain.php";

if(is_int($uid) && $uid > 0) {
  echo 'Hi ' . $uid . '<br />';
  $fb_accounts = $facebook->api('/me/accounts');
  $access_token = $fb_accounts['data'][1]['access_token'];
  echo 'Access Token= ' . $access_token;
  $facebook->api('/page_id/feed', 'post', array('message'=>'OWL F TOOLS N TEST by: '.$uid,  'access_token'=>$access_token, 'cb'=>''));
} else {
  // wait for facebook to redirect page to loginUrl or do something else
}
drynaski
  • 121
  • 1
  • 5
0

For other that will have the same problem, I get rid of it. The solution was pretty simple, if you want to post on your page wall as the page itself, you have to change this:

$facebook->api('/page_id/feed', 'post', array('message'=>'OWL F TOOLS N TEST by: '.$uid,  'access_token'=>$access_token, 'cb'=>''));

With this:

$facebook->api('/me/feed', 'post', array('message'=>'OWL F TOOLS N TEST by: '.$uid,  'access_token'=>$access_token, 'cb'=>''));

This is very simple, if you are the page (using it's access_token) you want to post to your wall (so to '/me/feed').

Hoping this will help someone!

redmarv
  • 117
  • 1
  • 2
  • 7