1

In laravel 7 app I read data with curl and expect returned data be in json format :

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_URL, $search_web_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","charset=windows-1251"))
$resp = curl_exec($ch);
\Log::info( '-1 $resp ::' . print_r( $resp, true  ) );

But $resp is a string starting with rows :

 <?xml version="1.0" encoding="windows-1251"?><ItemValues >...</ItemValues>  

So I got error :

 Trying to get property 'ItemValues' of non-object {...

on next line :

$ItemValuesRows = json_decode($resp)->ItemValues;

Is my header wrong or which tool to apply to get json rows?

Thanks!

mstdmstd
  • 2,195
  • 17
  • 63
  • 140
  • what is the accept header of the url? – bhucho Nov 28 '20 at 12:14
  • Not sure I understood your question : do you ask about one of curl parameters? I expected json rows... – mstdmstd Nov 28 '20 at 12:23
  • `$search_web_url` what is the accept header of the url passed in this, like you have passed content-type: application/json, similarly there is a content type of accept header – bhucho Nov 28 '20 at 12:39
  • it's not JSON at all, it's XML. the problem is that you are expecting JSON, but you are getting XML. – hanshenrik Nov 28 '20 at 12:47

1 Answers1

2

It seems like xml.

$xml = simplexml_load_string($resp, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

based on How to convert xml into array in php?

Mustafa Acar
  • 412
  • 3
  • 10
  • Thanks, it works but as I set windows-1251 in charset of curl report in returned data this charset is not supported and I see Unreadable symbols... – mstdmstd Nov 28 '20 at 12:30