如何发送 JSON 并使用 cURL 检索它?
How to send JSON and retrieve it using cURL?
这个link = http://localhost/api_v2/url?key=***
将 return 此列表 JSON :
我已经测试过:
- 通过浏览器导航到
http://localhost/api_v2/url?key=***
- 并通过命令行发出 curl 请求
$ curl http://localhost/api_v2/url?key=***
- 两种方法都会给 return JSON,并给我相同的结果。
- 嗯,据我所知,我的
$array
正在存储一些东西。
- 即使我这样做了
dd($array)
- 我仍然得到相同的结果。
这是我的
以下是我如何建立 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);
^^^^^^
这个link = http://localhost/api_v2/url?key=***
将 return 此列表 JSON :
我已经测试过:
- 通过浏览器导航到
http://localhost/api_v2/url?key=***
- 并通过命令行发出 curl 请求
$ curl http://localhost/api_v2/url?key=***
- 两种方法都会给 return JSON,并给我相同的结果。
- 嗯,据我所知,我的
$array
正在存储一些东西。 - 即使我这样做了
dd($array)
- 我仍然得到相同的结果。
这是我的
以下是我如何建立 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);
^^^^^^