0

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

2 Answers2

0

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

$sms_status = 'success' . $js->messages[0]->{'message-id'};
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Or change message-id to message_id, like this

$json = '
{
      "message-count": 1,
      "messages": [
        {
          "to": "447700900000",
          "message_id": "0A0000000123ABCD1",
          "status": "0",
          "remaining-balance": "3.14159265",
          "message-price": "0.03330000",
          "network": "12345"
        }
      ]
    }
';

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

echo $sms_status;
ild flue
  • 1,323
  • 8
  • 9
  • Cant change message- into message_. Because its a third party API where i do not have modification option. –  Jan 24 '18 at 21:30