I am trying to get the ID from the result. How I can print only ID?
I have tried decode $resp
but seems like the issue with array
.
//API Url
$url = 'https://api.clover.com:443/v3/merchants/'.$merchantid.'/orders/';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'state' => 'open',
'title' => '0010-W'
);
$headers = array(
'Content-type: application/json',
'Authorization: Bearer '.$authtoken.' ',
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //<=====This was missing.
curl_setopt($ch, CURLOPT_HEADER, false);
$resp = curl_exec($ch);
$data = json_decode($resp, true);
echo 'ID: '.$data['id'];
{
"href": "https://www.clover.com/v3/merchants/***/orders/D4MHB7M5D0P86",
"id": "D4MHB7M5D0P86",
"currency": "USD",
"title": "0010-W",
"taxRemoved": false,
"isVat": false,
"state": "open",
"manualTransaction": false,
"groupLineItems": true,
"testMode": false,
"createdTime": 1553976014000,
"clientCreatedTime": 1553976014000,
"modifiedTime": 1553976014000
}
I expect to get the ID, but I get empty result.
The question that mentioned as duplicate is explaining different thing not cURL data.
Resolved - The question edited with the answer. CURLOPT_RETURNTRANSFER Needs to be set in order to print the data. Tested and work perfectly.