Search code examples
phpjsonsmsvonage

PHP - get value from json object?


How do i get the value of message-id from json object? When i do $js->messages[0]->message-id then i get HTTP ERROR 500

Curl data:

{
      "message-count": 1,
      "messages": [
        {
          "to": "447700900000",
          "message-id": "0A0000000123ABCD1",
          "status": "0",
          "remaining-balance": "3.14159265",
          "message-price": "0.03330000",
          "network": "12345"
        }
      ]
    }

PHP:

$js = json_decode($result);   
if ($js->messages[0]->status =="0") {
   $sms_status = 'success' . $js->messages[0]->message-id;
}

Solution

  • The - is not a valid character for PHP variables. Use this syntax:

    $sms_status = 'success' . $js->messages[0]->{'message-id'};