Accessing the results from CURL request - php

Im making a CURL request in PHP, and trying to echo the ID result.
$responseData = request();
echo $responseData["id"];
The results are
{
"result":{
"code":"000.200.100",
"description":"successfully created checkout"
},
"buildNumber":"3b5605d6df9e6068ff1e9b178947fc41e641456e#2019-11-26 03:42:27 +0000",
"timestamp":"2019-11-27 13:19:29+0000",
"ndc":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02",
"id":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02"
}
But when trying to get the ID i am getting the error
Warning: Illegal string offset 'id' in \public\payment\index.php on line 25

JSON can easily be parsed in PHP using json_decode(). There is an optional flag to decode the JSON into an object or an array.
Decode as an object:
$json = request();
$data = json_decode($json);
echo $data->id;
Decode as an array:
$json = request();
$data = json_decode($json, TRUE);
echo $data['id'];

if you can decode you json using json_decode and then access the id variable like you are doing will be good for you.
$data = request();
$responseData = json_decode($data);
echo $responseData["id"];

Related

How to add Array Object and Data to JSON in Laravel

How to add Array Object and Data to JSON in Laravel
$json = {'id':5, 'name':'Hassan'};
I want to add new object role and value Admin to $json
I want result like
$json = {'id':5, 'name':'Hassan', 'role':'Admin'};
You could decode the JSON, add the values you want and then encode it back to JSON:
$data = json_decode($json, true);
$data['role'] = 'Admin';
$json = json_encode($json);
json_decode() Docs
json_encode() Docs
Try this
$object = json_decode($json);
$object->role = 'Admin';
$json = json_encode($object)

I am doing CRUD operation by using api.php from github and want to fetch id or name and save into a varibale

$response = call('GET','http://localhost/ProjectCamera/projectcamera/api.php/state');
$jsonObject = json_decode($response, true);
$jsonObject = php_crud_api_transform($jsonObject);
$output = json_encode($jsonObject, JSON_PRETTY_PRINT);
$state_id = $output->state_id;
print_r($state_id);
?>
Since $jsonObject is already an object you don't need to json_encode again with JSON_PRETTY_PRINT instead you should use json_decode($jsonObject) this will convert it to stdClass Object then you can access the state_id property/field.
you can try the below code.
$response = call('GET','http://localhost/ProjectCamera/projectcamera/api.php/state');
$jsonObject = json_decode($response, true);
$jsonObject = php_crud_api_transform($jsonObject);
//If $jsonObject is an JSON object otherwise
//If php_crud_api_transform is returning the array then you need to used json_encode but without JSON_PRETTY_PRINT
//BY passing true in json_decode second parameter will convert it to array
$output = json_decode(json_encode($jsonObject),true);
//you will get first state id
$state_id = $output['state'][0]['state_id'];
print_r($state_id);

Problems parsing JSON with PHP (CURL)

I am trying to access the value for one of the currencies (e.g. GBP) within the "rates" object of the following JSON file:
JSON file:
{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}
This was my approach:
PHP (CURL):
$url = ...
// initialize CURL:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// get the (still encoded) JSON data:
$json = curl_exec($ch);
curl_close($ch);
// store decoded JSON Response in array
$exchangeRates = (array) json_decode($json);
// access parsed json
echo $exchangeRates['rates']['GBP'];
but it did not work.
Now, when I try to access the "timestamp" value of the JSON file like this:
echo $exchangeRates['timestamp'];
it works fine.
Any ideas?
Try removing (array) in front of json_decode and adding true in second parameter
Here is the solution.
$exchangeRates = (array) json_decode($json,TRUE);
https://ideone.com/LFbvUF
What all you have to do is use the second parameter of json_decode function.
This is the complete code snippet.
<?php
$json='{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}';
$exchangeRates = (array) json_decode($json,TRUE);
echo $exchangeRates["rates"]["GBP"];
?>

Parse Data from JSON URL

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.

get Specific JSON Response Value

json response as below:
{"Success":true,"ErrorCode":0,"UserInformation":{"UserID":"19"}......
how do I get the UserID value by using json_decode?
I tried the below code with no luck.
$Response = json_decode($Response[2]);
echo $Response[0][0][0];
This should work:
<?php
$json = '{"Success":true, "ErrorCode":"0","UserInformation":{"UserID":"19"}}';
$response = json_decode($json, true);
echo $userId = $response["UserInformation"]["UserID"];
?>
Please check the PHP documentation of json_decode at http://php.net/json_decode
You can use object as well if you skip the second parameter of the json_decode function.

Categories