Json Help

Hi

I have the following Json code i need to extract the message ID and status from it

Array
(
    [test_mode] => 1
    [balance] => 150
    [batch_id] => 0
    [cost] => 1
    [num_messages] => 1
    [message] => Array
        (
            [num_parts] => 1
            [sender] => 447777777777
            [content] => 447777777777
        )

    [receipt_url] => 
    [custom] => 
    [messages] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [recipient] => 0
                )

        )

    [status] => success
)

how would i extract it as i was using the below but i think the API has been updated with a slightly different response.

// Process your response here
	$jsonData = json_decode($response);

if ($jsonData->status === 'success') {
    
	// Grab Message ID
	foreach($jsonData->messages as $txtid)
  {
   // Got Message ID
   // Success code
	echo ('Sent: message to '.$to.' from '.$txtid.'');
  }  

Make it an array so it’s easier to work with:


$response = json_decode($response, true);

It looks like this API has the possibility of returning multiple messages to you, so I don’t think you just want to statically retrieve the first one.


$response = json_decode($response, true);

$messageIds = array();
foreach($response['messages'] as $message) {
    array_push($messageIds, $message['id']);
}

print_r($messageIds);

Side note, is this API for velti / air2web?

Cheers dude no mate it is www.textlocal.co.uk their api for sending SMS :slight_smile:

I will look in to that bit of code you have given now :slight_smile: