1

I've created this code to show me data from a URL GET, putting the url alone, show me details in ARRAY. But when i insert the url in the script to get data, dont show me nothing, and when i put the Foreach, show me an error.

***I cannot give the API key (Client information Protection)

$curl = curl_init();

curl_setopt_array($curl, array(
 CURLOPT_URL => "https://retail.yclient.com/yclientapi/v2/KEY/GetClients",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "GET",
 CURLOPT_POSTFIELDS => "",
 CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "cache-control: no-cache"
 ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

$data = json_decode($response, false);

   foreach($data as $ckey => $client) {
       
       echo $client['Name']."<br/>"; 
       echo $client['Email']."<br/>"; 
       echo $client['CellPhone']."<br/>"; 
 
   }

curl_close($curl);

ERROR

Warning: Invalid argument supplied for foreach() in /home/cloudiu/www/assets/api_request_clients.php on line 34

Can someone tell me the reason for not showing results...

If I access the same URL from my browser, it returns

[{"RID":1,"ClientSalesRID":null,"AccountID":1,"TaxID":"","IdCard":"13731415","Name":"Pedro Pestana","Address":"","PostalCode":"","PostalCodeLastDigits":"","City":"Funchal","Country":"Portugal","CellPhone":"931314712","Phone":"","Email":"pdrpstn@gmail. 

Debugging:

var_dump($response) returns string(0) ""

var_dump($err) returns string(0) ""

Response Header from cURL shows

HTTP/1.1 401 Unauthorized 
Cache-Control: no-cache 
Pragma: no-cache 
Expires: -1 
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Wed, 29 Sep 2021 13:49:08 GMT 
Content-Length: 0
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Try change it to `json_decode($response, true)`. If you pass in `false` (which is the default), it will convert the response into an object, not an array. You should also check that you got an array back before using `foreach()` (`if (is_array($data)) { foreach(...) {...}}`. Never assume that the response is what you think. – M. Eriksson Sep 29 '21 at 12:49
  • As per your error, `$data` is not the array. can you please add `$response` output here? It might be possible that you don't need to decode the response. – KudosIntech Sep 29 '21 at 12:49
  • I try to echo response, tryied to echo data, and no data. The URL already give in ARRAY.... so.... where am i wrong? – Sede Ideias Saudáveis Sep 29 '21 at 12:53
  • By URL show me like that "[{"RID":1,"ClientSalesRID":null,"AccountID":1,"TaxID":"","IdCard":"13731415","Name":"Pedro Pestana","Address":"","PostalCode":"","PostalCodeLastDigits":"","City":"Funchal","Country":"Portugal","CellPhone":"931314712","Phone":"","Email":"pdrpstn@gmail.com","Sex":"M","Password":"","BirthDate":"1990-10-31T00:00:00","Job":"","SugestedBy":null,"PercentageTo":null,"PercentageToGive":null,"AcceptEmail":true,"AcceptSms":true,"AcceptSocial":true,"ActiveClient":true,"InvalidCellphone"A":" @KudosIntech – Sede Ideias Saudáveis Sep 29 '21 at 12:54
  • So you mean `var_dump($response);` returns nothing? If so, then what does `var_dump($err);` return? – ADyson Sep 29 '21 at 13:00
  • Even if $response is populated, then as the first comment says, you'd need to have `json_decode($response, true);` in order to then use `$data` in the way you're trying to. – ADyson Sep 29 '21 at 13:01
  • Demo using appropriately decoded JSON: http://sandbox.onlinephpfunctions.com/code/2ea71389d6cfba5fa46edbdc4a4114834862a085 . However this assumes that $response is correctly populated. – ADyson Sep 29 '21 at 13:06
  • yes i already tried TRUE on Array, and the VAR_DUMP show "String(0)" @ADyson – Sede Ideias Saudáveis Sep 29 '21 at 13:27
  • `the VAR_DUMP `...which one? I asked for two different ones, if you notice. (P.S. As my demo shows, you'll still need to make the `true` change, but before that you need to figure out why the cURL request isn't returning anything.) – ADyson Sep 29 '21 at 13:28
  • @ADyson i did both. But seems that the url is the problem. But when i put the url on Chrome, show me the same array that i put here... – Sede Ideias Saudáveis Sep 29 '21 at 13:30
  • `i did both`. Ok so please share the results of _both_ then... – ADyson Sep 29 '21 at 13:31
  • `seems that the url is the problem. But when i put the url on Chrome, show me the same array that i put here` ... in that case it's probably _not_ the URL, it's some issue with doing it from cURL specifically. Where are you running the PHP code? On your localhost, or on a server? If it's on a server, maybe the connection is blocked for some reason. Or maybe the cURL is just misconfigured somehow. It would also be useful to get the response header information back from cURL, there might be clues in there - see https://stackoverflow.com/a/9183272/5947043 for instructions. – ADyson Sep 29 '21 at 13:46

0 Answers0