I am writing a WP plugin. I am using json_decode($result, true). When I test connecting plugin on localhost (xampp) I use this code:
$result = curl_exec($ch);
$json = json_decode($result, true);
$access_token = $json["access_token"];
curl_close($ch);
The plugin works great, but if I put the plugin on a real site – I get a warning:
"Warning: Undefined array key "access_token" in /storage/conte…api/create-paypal-order.php on line 67"
Tried another json_decode($result) – if I test the plugin on localhost (xampp) and use the code:
$result = curl_exec($ch);
$json = json_decode($result);
$access_token = $json->access_token;
curl_close($ch);
The plugin works great, but if I put the plugin on a real site – I get a warning:
"Warning: Undefined property: stdClass::$access_token in /storage/cont…/includes/api/create-paypal-order.php on line 65"
How to fix these warnings?
2
Answers
Apply
isset($json->access_token)
I think this will help youfirst of all you need to print the json variable.
so you will get idea about access_token.
use
print_r()
function to print$json
variable.if the json data is in array format then use
$json['access_token']
if the json data is in Object format then use
$json->access_token
below is the code for Array type variable
below is the code for Object type Variable