在laravel 7应用程序中,我使用curl读取数据,并期望返回的数据为json格式:
$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 ) );
但是$ resp是一个以rows开头的字符串:
<?xml version="1.0" encoding="windows-1251"?><ItemValues >...</ItemValues>
所以我得到了错误:
Trying to get property 'ItemValues' of non-object {...
在下一行:
$ ItemValuesRows = json_decode($ resp)-> ItemValues;
我的标头是否错误或应使用哪种工具来获取json行?
谢谢!
好像是xml。
$xml = simplexml_load_string($resp, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
谢谢,它可以工作,但是当我在返回数据的curl报告的字符集中设置Windows-1251时,不支持此字符集,并且看到了无法读取的符号...