0

I am working on creating div for each result coming from curl_exec json GET request. I have tried the code below using cURL in PHP. When I run this code nothing comes from the foreach.

    //API Url
    $url = 'https://api.clover.com:443/v3/merchants/'.$merchantid.'/categories/';

    //Initiate cURL.
    $ch = curl_init($url);

    $headers = array(
        'Content-type: application/json',
        'Authorization: Bearer '.$authtoken.' ',
    );

    //Tell cURL that we want to send a GET request.
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    //Execute the request
    $resp = curl_exec($ch);
    $respArray = json_decode($resp, true);

    foreach($respArray as $key => $value) {
      echo $key . " : " . $value;
    }
    curl_close($ch);

This returns the following:

   {
      "elements": [
        {
          "id": "QWS8MSMAT49E6",
          "name": "Other",
          "sortOrder": 2
        },
        {
          "id": "35K000MJVVFHE",
          "name": "Pizza",
          "sortOrder": 1
        }
      ],
      "href": "http://api.clover.com/v3/merchants/{[MID]}/categories?limit=100"
    }

I'm trying to get the information above into a div, for example:

example output

10
  • Do u get any error or notice? It looks like u process the array $respArray in the foreach loop. But you should apply $respArray["elements"]. Commented Jan 13, 2019 at 21:02
  • @LucaJung I have just checked PHP log and found PHP Warning: Invalid argument supplied for foreach(). When you say I should apply $respArray["elements"] what do you mean? where the change will be on my code? I am might missing the point of it. Thank you for your response. Commented Jan 13, 2019 at 21:08
  • Can you post the output of var_dump($respArray);? I mean change this line foreach($respArray as $key => $value) to foreach($respArray["elements"] as $key => $value). Commented Jan 13, 2019 at 21:10
  • @LucaJung Getting int(1) from var_dump($respArray); Commented Jan 13, 2019 at 21:11
  • If you get int(1) back, where do you get the returning information from? Commented Jan 13, 2019 at 21:13

2 Answers 2

0

think it works like this:

$data = json_decode($resp);
foreach($data->elements as $element) {
  echo '<div id="group_'.$element->id.'" class="product_group">'.$element->name.'</div>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is still returning nothing. And PHP log still showing PHP Warning: Invalid argument supplied for foreach()
@Profile this most likely is because of json_decode($resp, true). most easy would be to use xdebug and then set a break-point after the json_decode() instruction. see php.net/manual/en/function.json-decode.php
You was right with removing TRUE from json_decode() . With setting RETURNTRANSFER TRUE and removing TRUE from json_decode did work. Thank you!
0

I have resolved the issue with adding

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

To the code. As has been explained Here (by Mihai)

"By default curl_exec outputs the data it gets from the server to the standard output so your $response variable doesn't really have the actual response data in it. If you want to get the data in a variable set the CURLOPT_RETURNTRANSFER option before calling curl_exec."

And Martin Zeitler's Answer was right with removing true from json_decode()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.