Gaz Smith
Gaz Smith

Reputation: 1108

Accessing the results from CURL request

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

Upvotes: 1

Views: 52

Answers (2)

Amit Sharma
Amit Sharma

Reputation: 1795

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"];

Upvotes: 0

David
David

Reputation: 344

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'];

Upvotes: 1

Related Questions