0

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.

Profile
  • 23
  • 1
  • 7
  • Try changing `$data = json_decode($resp);` to `$data = json_decode($resp, true);`. – Xxmarijnw Mar 30 '19 at 20:42
  • Change `$resp['id'];` to `$data['id'];` ...you decoded the data but thens didn't use it, and tried to access the un-decoded version instead. – ADyson Mar 30 '19 at 21:34
  • @Xxmarijnw still not working. – Profile Mar 30 '19 at 21:58
  • @ADyson you are right my bad. But still nothing is showing. – Profile Mar 30 '19 at 21:58
  • 1
    If `$resp` does indeed contain the data shown, then your code works - here's a demo: http://sandbox.onlinephpfunctions.com/code/f45d812245eec02e04fdd23846f9da76b5ceb226 . So I can only assume that either a) the cURL call is failing, or b) the data in $resp is not the data you've shown us, or c) somehow you are not looking in the right place for the output? – ADyson Mar 30 '19 at 22:10
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – miken32 Mar 30 '19 at 22:32
  • @miken32 the extracting data part is fine, assuming the data being supplied is what is stated in the question...see my last comment for proof. I don't think that's the issue, so the duplicate doesn't answer it. If OP is getting no output then the problem is elsewhere, as I've described – ADyson Mar 30 '19 at 22:46
  • 1
    @ADyson you was right with `$data` is not including the correct data. After adding `CURLOPT_RETURNTRANSFER` $data now showing needed data. – Profile Mar 31 '19 at 02:35
  • @miken32 the issue was with cURL not with JSON. – Profile Mar 31 '19 at 02:36

0 Answers0