如何发送 JSON 并使用 cURL 检索它?

How to send JSON and retrieve it using cURL?

这个link = http://localhost/api_v2/url?key=***

将 return 此列表 JSON :

我已经测试过:

这是我的


以下是我如何建立 JSON

public function index_2(){

    $file_name = 'inventory.csv';
    $file_path = 'C:\QuickBooks\'.$file_name;
    $csv= file_get_contents($file_path);
    $utf8_csv = utf8_encode($csv);
    $array = array_map("str_getcsv", explode("\n", $utf8_csv));

    return Response::json($array);

}

下面是我如何发出 cURL 请求并尝试检索 JSON

<?php 

    $ch = curl_init("http://localhost/api_v2/url?key=***");
    curl_setopt($ch, CURLOPT_USERPWD, "admin:*****");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $body = curl_exec($ch);
    curl_close($ch);

    $json_decode = json_decode($array, TRUE);

我一直抱怨 $array 变量没有定义,但事实上我确实像这样定义并发送了它 return Response::json($array); .

我不确定我在这里做错了什么。

找出不同之处:

$body = curl_exec($ch);
^^^^^

$json_decode = json_decode($array, TRUE);
                           ^^^^^^