【问题标题】:JSon decode doesnt parse the response into a arrayJSon decode 不会将响应解析为数组
【发布时间】:2016-03-02 01:08:40
【问题描述】:

我正在尝试解析从服务器到数组的 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 在我打印后出现在字符串的末尾。

有人对我有什么建议吗?

【问题讨论】:

    标签: php arrays json curl server


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2011-05-20
      • 2021-11-13
      • 2021-08-15
      • 2013-09-19
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多