简体   繁体   English

PHP - cURL 注意:数组到字符串的转换

[英]PHP - cURL Notice: Array to string conversion in

When I try and grab a value from the cURL response.当我尝试从 cURL 响应中获取值时。 I am getting a Illegal string offset 'type' in .Illegal string offset 'type' in . When I remove the index value and just do $resp_orders .当我删除索引值并执行$resp_orders It returns the whole response fine.它可以很好地返回整个响应。

$ch = curl_init('https://api.companieshouse.gov.uk/company/<companynumber>');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_POSTREDIR, 3);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow http 3xx redirects
        curl_setopt($ch, CURLOPT_USERPWD, "AUTH_KEY" . ":" . "");
        $resp_orders = curl_exec($ch); // execute

        print_r($resp_orders['company_name']);

curl_exec() is going to return a string. curl_exec() 将返回一个字符串。 Assuming the result back is suppose to be a JSON result, then you need to json_decode it an array:假设返回的结果是 JSON 结果,那么您需要将其 json_decode 为一个数组:

$resp_orders_json = curl_exec($ch); $resp_orders_json = curl_exec($ch);

$resp_orders = json_decode($resp_orders_json, true); $resp_orders = json_decode($resp_orders_json, true);

I think instead of: print_r($resp_orders['company_name']);我认为而不是: print_r($resp_orders['company_name']);

remove ['company_name'] just print_r($resp_orders);删除 ['company_name'] 只是 print_r($resp_orders);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM