如何通过我的 Web Api 访问 PHP 中返回的 json?
How can I access the json returned in PHP by my Web Api?
我正在使用以下代码从我的网站发送和检索数据API
//data
$data = array("Id_Empresa" => 1);
try {
$ch = curl_init($url);
$data_string = json_encode($data);
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
var_dump($data);
$json = json_decode($data);
foreach ($json->msg as $item) {
echo "$item->Nombre, $item->Descripcion" . PHP_EOL;
}
// ...process $output now
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
这是我得到的回应
{"ok":true,"msg":[{"Nombre":"Carnicerias","Descripcion":"Comercio al por menor de carnes rojas","H_Open":"01:00:00","H_Close":"02:00:00"}]}bool(true)
我正在尝试使用以下代码访问 JSON(因为它对我的类似请求有效):
$json = json_decode($data);
foreach ($json->msg as $item) {
echo "$item->Nombre, $item->Descripcion" . PHP_EOL;
}
但是正如您所看到的,变量 $data 不再是 JSON 而是变成了 bool(true)。
有谁知道我如何访问 JSON msj 或为什么 $data 变量从 JSON 更改为 bool?
curl_exec
PHP 手册的 return 值部分说
Returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure.
也许它可以更具体 - 该选项必须设置为 true
。请参阅 curl_setopt
文档中选项的定义:
true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
所以,您会看到 JSON 响应,因为它是由 curl_exec
直接输出的,然后是 bool(true)
,因为 curl_exec
已经 returned true
到您要转储的 $data
变量。
将 CURLOPT_RETURNTRANSFER
设置为 true
以获得您所期望的结果。
我正在使用以下代码从我的网站发送和检索数据API
//data
$data = array("Id_Empresa" => 1);
try {
$ch = curl_init($url);
$data_string = json_encode($data);
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
var_dump($data);
$json = json_decode($data);
foreach ($json->msg as $item) {
echo "$item->Nombre, $item->Descripcion" . PHP_EOL;
}
// ...process $output now
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
这是我得到的回应
{"ok":true,"msg":[{"Nombre":"Carnicerias","Descripcion":"Comercio al por menor de carnes rojas","H_Open":"01:00:00","H_Close":"02:00:00"}]}bool(true)
我正在尝试使用以下代码访问 JSON(因为它对我的类似请求有效):
$json = json_decode($data);
foreach ($json->msg as $item) {
echo "$item->Nombre, $item->Descripcion" . PHP_EOL;
}
但是正如您所看到的,变量 $data 不再是 JSON 而是变成了 bool(true)。
有谁知道我如何访问 JSON msj 或为什么 $data 变量从 JSON 更改为 bool?
curl_exec
PHP 手册的 return 值部分说
Returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure.
也许它可以更具体 - 该选项必须设置为 true
。请参阅 curl_setopt
文档中选项的定义:
true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
所以,您会看到 JSON 响应,因为它是由 curl_exec
直接输出的,然后是 bool(true)
,因为 curl_exec
已经 returned true
到您要转储的 $data
变量。
将 CURLOPT_RETURNTRANSFER
设置为 true
以获得您所期望的结果。