我正在尝试将服务器的JSON响应解析为数组,但它不起作用。
我在用:
json_decode($string,true);
所以这不应该是问题。
这是我向服务器询问的代码:
$curl_header = array();
$curl_header[] = "Authorization: Basic ".base64_encode("$auth_code:");
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl, CURLOPT_URL, 'https://api.kassacompleet.nl/v1/ideal/issuers/');
$result = curl_exec($curl);
curl_close($curl);
// Handle and store result
$test = json_decode($result, true);
print_r($test);
这是我从服务器获得的响应,并在打印后:
[ { "id": "INGBNL2A", "list_type": "Nederland", "name": "Issuer Simulation V3 - ING" }, { "id": "RABONL2U", "list_type": "Nederland", "name": "Issuer Simulation V3 - RABO" } ]1
打印后,这个奇怪的nr 1也出现在字符串的末尾。
有人对我有任何提示吗?
答案 0 :(得分:1)
$result
可能是1
,因为这是curl_exec返回的内容。 HTTP响应正在发送到PHP的输出流,因为您需要使用选项CURLOPT_RETURNTRANSFER
将结果返回到变量中。
试试这个:
$curl_header = array();
$curl_header[] = "Authorization: Basic ".base64_encode("$auth_code:");
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl, CURLOPT_URL, 'https://api.kassacompleet.nl/v1/ideal/issuers/');
// need to add this option to have curl_exec return the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl); // result is now the response, before it was `true`
curl_close($curl);
// Handle and store result
$test = json_decode($result, true);
print_r($test);