Home > database >  How to parse json in php when json_decode, stripslashes and other solutions are not working?
How to parse json in php when json_decode, stripslashes and other solutions are not working?

Time:12-15

I know this type of question has been asked multiple times on this forum, but I have looked at countless variations of the question and have tried many different solutions and none of it is working, so I thought I would paste my actual code in case I am totally missing something. So I am accessing an API for a CRM and receiving json output. The output is seen as below:

{"access_token":"415db7610058604900566bdd","expires":1639406913,"expires_in":3600,"scope":"granted_permission","domain":"bitrix24.com","server_endpoint":"https:\/\/oauth.bitrix.info\/rest\/","status":"L","client_endpoint":"https:\/\/bitrix24.com\/rest\/","member_id":"**************","user_id":8,"refresh_token":"31dcde610058604900566bdd00000008f0f1037319823fdbcad5b438d3098146949075"}

This is the output I get after using cURL to post a URL that then allows a refresh_token to be generated, which can be seen in the json output. I have to parse the json in order to get the new refresh token so that I can re-enter it when the token has expired.

After research I learned that you can parse json via PHP by using the json_decode function, I have tried this in many different ways, trying the function with trim, true, stripslashes and many more variations.

When I run the below code I can echo the $result variable and see the json, but when I try and echo just the refresh_token which I have parsed from the json output, all I get is a blank screen, no matter which method I try and use or even if I var_dump. If someone could explain what I have done wrong, whether I am supposed to be importing some sort of external library or whether I have missed a step somewhere, that would be greatly appreciated.

<?php header("Content-Type: application/json; charset=UTF-8");
ini_set("allow_url_fopen", 1);

$client_id = "**********";
$secret_id = "****************";
$refresh_token = "94dade610058604900566bdd00000008f0f103f252c645ad3779a511ca2346b2fe9f27";
$refresh_token2 = "";


if($refresh_token2 == "") {
    $url = "https://bitrix24.com/oauth/token/?grant_type=refresh_token&client_id=" . $client_id . "&client_secret=" . $secret_id . "&refresh_token=" . $refresh_token . "&scope=granted_permission&redirect_uri=app_URL";

    // Initialize a CURL session.
    $ch = curl_init();

    // Return Page contents.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //grab URL and pass it to the variable.
    curl_setopt($ch, CURLOPT_URL, $url);

    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
    

    $json = file_get_contents($result);
    $obj = json_decode($json, true);
    var_dump($obj);


}


Here I tried @dietcheese solution. I also added a screenshot of the output showing how it doesn't output the json value. This is what I meant by I have tried solutions posted on the forum before (@dietcheese solution being of one them) and they do not work.

<?php header("Content-Type: application/json; charset=UTF-8");
ini_set("allow_url_fopen", 1);

$client_id = "**************";
$secret_id = "**************";
$refresh_token = "250be0610058604900566bdd";
$refresh_token2 = "";


if($refresh_token2 == "") {
    $url = "https://bitrix24.com/oauth/token/?grant_type=refresh_token&client_id=" . $client_id . "&client_secret=" . $secret_id . "&refresh_token=" . $refresh_token . "&scope=granted_permission&redirect_uri=app_URL";

    // Initialize a CURL session.
    $ch = curl_init();

    // Return Page contents.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //grab URL and pass it to the variable.
    curl_setopt($ch, CURLOPT_URL, $url);

    $result = curl_exec($ch);
    echo "This is the result variable being echoed: \n\n" . $result;
    curl_close($ch);
    
    $json = json_decode($result, true);
    echo "\n\n\nHere the json should print: \n\n";
    print_r($json);
    echo "\n\nHere the refresh token should echo: \n\n";
    echo $json['refresh_token'];


}

Below is the output: enter image description here

This is why I asked the question because I am genuinely confused as to how nothing is working.

CodePudding user response:

Why are you using

$json = file_get_contents($result);

After your curl_close($ch) statement, try adding:

$json = json_decode($result, true);
print_r($json);

If that works, you should be able to do something like:

echo $json['refresh_token'];

CodePudding user response:

So I found the issue. Thank you to @ÁlvaroGonzález for his reminder that I should have debugging active in my codebox. (I know a rookie move, but I am still a rookie).

After enabling debugging, I found that PHP did not even recognise json_decode. After then installing the JSON module for PHP, everything works.

  • Related