0

EDIT

Please note, my issue is with regard to the url not returning any data at all....not about the API version as mentioned and answered in the 'duplicate' question' and as occurs later in my code. That could be another question.

So please, I need votes to reopen this question !

Original Question

Hi there are a few questions on SO on this. But none specifically or generically addresses my issue. So here i am.

I am trying to create a facebook page/group post script in PHP. The specific part of code I am mentioning here relates to a larger code that is supposed to list the pages of the facebook account and create a dropdown so the required page to be posted to can be selected.

I have the API set up. I have the SDK. I have the codes.

The issue is, when I execute the code to get the access toke vide Curl (file_get_contents returns permission denied error), I get a blank in the response.

The code is as follows;

 $token_url = "https://graph.facebook.com/oauth/access_token?"
        . "client_id=".$config['appId']."&redirect_uri=" . urlencode($config['callback_url'])
        . "&client_secret=".$config['secret']."&code=" . $_GET['code'];

    echo $token_url."<br>";
//    $response = file_get_contents($token_url);   // get access token from url

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $token_url);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec ($ch);

    curl_close ($ch); 

    echo "RESPONSE :".print_r($response);

    $params = null;

    parse_str($response, $params);

    $_SESSION['token'] = $params['access_token'];

//  echo "<script>alert(SESSION TOKEN : $_SESSION[token])</script>":
    echo "SESSION TOKEN : ".$_SESSION['token']."<br>";  

echoing $response and Print_r gives a blank.

The response on screen is

RESPONSE :

Notice: Undefined index: access_token in /home/u154857672/public_html/admin/fb/getlist.php on line 35

SESSION TOKEN :

Notice: Trying to get property of non-object in /home/u154857672/public_html/admin/fb/getlist.php on line 77

Where am I going wrong ? What other info is required to elaborate the problem ?

user3526204
  • 509
  • 5
  • 22
  • The return format of the access token endpoint has changed with API v2.3, you can not use `parse_str` on it any more, you need to use `json_decode` instead. – CBroe May 24 '17 at 11:16
  • @CBroe does that mean I have to use json_encode($config['callback_url']) instead of urlencode($config['callback_url']) ? – user3526204 May 24 '17 at 11:34
  • No, of course not, that would not make any sense whatsoever. Did I or did I not specifically refer to `parse_str`? – CBroe May 24 '17 at 11:36
  • forgive me as i am a noob at this. But isn't print_r ($response) before the parse_str supposed to output something ?. – user3526204 May 24 '17 at 11:39
  • If you actually got something returned, then maybe - but if making that request using file_get_contents got you a 403 response, then sending the exact same request using cURL is probably not going to change much ... How about you start by calling that exact URL in your browser, and see what that results in? – CBroe May 24 '17 at 11:43
  • I am getting error { "error": { "message": "This IP can't make requests for that application.", "type": "OAuthException", "code": 5, "fbtrace_id": "Av97BVsqqL4" } } – user3526204 May 24 '17 at 11:44
  • what does this mean ? – user3526204 May 24 '17 at 11:45
  • Most likely that you have restricted the IPs that can make API calls for this app via the setting in the app dashboard. – CBroe May 24 '17 at 12:08
  • Exactly ! I just got that and resolved by the time you were typing this. Thanks anyways :) – user3526204 May 24 '17 at 12:08
  • Now I am getting the access token. This i get when i directly put the url in the browser. Only thing is, print_r still doesn't print it. I am trying to debug that. – user3526204 May 24 '17 at 12:11

0 Answers0