Warm tip: This article is reproduced from serverfault.com, please click

laravel-为什么用curl读取数据我得到字符串值?

(laravel - Why read data with curl I get string value?)

发布于 2020-11-28 11:44:56

在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行?

谢谢!

Questioner
mstdmstd
Viewed
11
Mustafa Acar 2020-11-28 20:13:02

好像是xml。

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

基于如何将xml转换为php中的数组?