Nevzat Uz
Nevzat Uz

Reputation: 17

getting json values into php variable issue

i try to use some sms api and i have to get message-id. First of all here is the my api code.

$url = 'https://rest.nexmo.com/sms/json?' . http_build_query(
    [
      'api_key' =>  'xxx',
      'api_secret' => 'xxx',
      'to' => 'xxx',
      'from' => 'xxx',
      'text' => 'xxx'
    ]
);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

When i use this code it's working for sending sms and return response. And here is the json response.

{ "message-count": "1", "messages": [{ "to": "905075214049", "message-id": "0C000000440A086C", "status": "0", "remaining-balance": "1.52250000", "message-price": "0.01910000", "network": "28603" }] }

And i use the print_r function to see the structrue of json data. And here it's.

stdClass Object
(
    [message-count] => 1
    [messages] => Array
        (
            [0] => stdClass Object
                (
                    [to] => xxx
                    [message-id] => 0C000000440A086C
                    [status] => 0
                    [remaining-balance] => 1.52250000
                    [message-price] => 0.01910000
                    [network] => 28603
                )

        )

)

I use this code to get "message-id" into my php value.

$json_decode = json_decode($response);
$sonuc->messages[0]->message-id;

My issue is; if i try to get network section from this json it works. But if i try to get "message-id" section it returns only 0 on the screen. I really don't know how can i get full message-id. Please help.

Upvotes: 0

Views: 45

Answers (2)

maximkou
maximkou

Reputation: 5332

- char is not allowed for using as property or variable name. If you want get this property, do it like this:

$sonuc->messages[0]->{"message-id"}

Upvotes: 3

Dan Hastings
Dan Hastings

Reputation: 3280

You are using the wrong variable name. Try using

$sonuc = json_decode($response);
echo $sonuc->messages[0]->message-id;

Upvotes: 0

Related Questions