-3

I try to get data from a JSON URL via PHP & curl. how can I decode the "message" field? "status" works great with $result->status

Does anyone have an idea for me?

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));




?>

Edit: Answers works but not on my script .. any ideas why i get no response on "message" ?

Veltins
  • 55
  • 2
  • 8

1 Answers1

1

Assume $test is response

$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);
Ritesh Dhoke
  • 169
  • 10
  • hmmm thats all correct .. i have tested your code .. but on my script it dont works ...can you please check my first post again .. i will add my full code – Veltins Feb 18 '19 at 14:05