php - Php Json 解码 CURL
问题描述
我尝试通过 PHP & curl 从 JSON URL 获取数据。如何解码“消息”字段?“状态”适用于$result->status
有人对我有想法吗?
API JSON Format
"messages":
[
{
"id":30,
"message":"Service ID not provided!"
},
{
"id":32,
"message":"Service does not exist."
}
]
<?php
$url = 'https://www.youtube.com/watch?v=';
$quantity = '10';
$id_service = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.kkkkkkkkk.com/v1-api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Incase things are slow allow enough time to try.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Can cause unexpected/no return
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// Can cause unexpected/no return
$data = array(
/* Required */
'api_key' => "22",
'action' => 'add',
'url' => $url,
'quantity' => $quantity,
'id_service' => $id_service,
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = json_decode(curl_exec($ch));
?>
编辑:答案有效,但不适用于我的脚本..有什么想法为什么我对“消息”没有回应?
解决方案
假设 $test 是响应
$test = '{
"status":"success",
"service":"123456",
"url":"jofNR_WkoCE",
"long_url":"https://www.youtube.com/watch?v=",
"message":
[
{
"id":100,
"message":"Successfully added URL"
}
]
}';
$result = json_decode($test);
print_r($result->message[0]->id);