anitg 0 Newbie Poster

I am trying to get access toke from FB through Developer App. But I am not able to json_decode the call back response.

If I use the token_url call from browser, it returns the access token. But from within the script it does work.

I searched online and foun that it could be that the Json encoded response must be containg some UTF -8 BOM. So I tried some functions that would strip the response from that. However, I still am not able to get that elusive access token as a variable by json decoding and getting an array.

My code

<?php
function remove_utf8_bom($data)
{
    $bom = pack('H*','EFBBBF');
    $data = preg_replace("/^$bom/", '', $data);
    return $data;
}                   
                    $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>";                 
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $token_url);
                    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        //          curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                    $data = curl_exec ($ch);
                    if(curl_exec($ch) === false) {
                        echo 'Curl error: ' . curl_error($ch);
                    } else 
                    {
                        echo 'Operation completed without any errors<br>';
                    }

//                  $data=file_get_contents($token_url);                    
    //              echo $data."<br>";
                    $data = remove_utf8_bom($data);
//                  echo "<br> DATA : <br>";
    //              var_dump($data);
                    $params = null;
                    $params=json_decode($data,true);
                    print_r ($params)."<br>";
                    $_SESSION['token'] = $params['access_token'];

                    echo "SESSION TOKEN : ".$_SESSION['token']."<br>";
?>                  

But still it returns NULL

I have been breaking my head and browsing all kinds of forums but could not get any solution.

Thanks in advance if you can put me out of my misery

Dani AI

Generated

Most likely causes (from ’s snippet)

  • The OAuth endpoint often returns an URL-encoded query string (e.g. access_token=...&expires=...) rather than JSON, so json_decode() will return NULL.
  • The code calls curl_exec() twice (once into $data, then again inside the if), which re‑runs the request and makes error checking wrong.
  • print_r($params)."&lt;br&gt;" concatenates a non‑string return value and can hide what was actually parsed. Also confirm session_start() ran before writing $_SESSION.

Minimal, reliable fixes

  • Capture and check the single curl_exec() result, then inspect it (var_dump or log).
  • If the response is URL-encoded, use parse_str() to get access_token. If JSON is needed, request it via an Accept header and then json_decode().

Example (minimal):

if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
if ($data === false) {
  error_log('curl error: ' . curl_error($ch));
  curl_close($ch);
  // handle error
}
curl_close($ch);

parse_str($data, $params);
if (!empty($params['access_token'])) {
  $_SESSION['token'] = $params['access_token'];
}

Extra troubleshooting and cautions

  • Verify the built URL actually uses & (not an HTML-escaped &amp;) and that redirect_uri exactly matches the app settings.
  • Inspect the raw $data for error/error_description.
  • To force JSON responses, add an Accept: application/json header to the request or use the official Facebook PHP SDK to avoid low-level mistakes.
  • Do not disable SSL verification in production; only relax it temporarily for testing if necessary.

These targeted checks typically explain why json_decode() returned NULL and will yield the access token reliably.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.