【问题标题】:Php Json decode CURL [duplicate]Php Json解码CURL [重复]
【发布时间】:2019-02-18 13:36:36
【问题描述】:

我尝试通过 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));




?>

编辑:答案有效,但不适用于我的脚本..任何想法为什么我在“消息”上没有得到回应?

【问题讨论】:

  • 了解对象和数组之间的区别,然后这个问题就已经回答了。 $result-&gt;message[0]-&gt;message
  • 试试$result-&gt;message[0]-&gt;id

标签: php json curl decode


【解决方案1】:

假设 $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);

【讨论】:

  • 嗯,这都是正确的..我已经测试了你的代码..但是在我的脚本上它不起作用......你能再检查一下我的第一篇文章吗..我会添加我的完整代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多