1

I'm just wondering, is this correct? I can't find any guide for doing it this way, but I don't know how many elements regIds will have, so it seems like it needs to be done like this. regIds is the array.

$fields = array(
       'registration_ids' => $regIds,
         'data' => array( "message" => $message ),
        ); 

Edit: full script with sensitive values omitted:

<?php
$name = $_POST['name'];
$message = $_POST['message'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];

$url = "dburl";
$username = "dbusername";
$password = "password";
$db = "database";
mysql_connect($url, $username, $password);
mysql_select_db($db);
$sql = mysql_query("select chatId from Players natural join Accounts where account_state = 'O' and ABS(playerLat - '$latitude') < 0.1 and ABS(playerLong - '$longitude') < 0.1");
while($row = mysql_fetch_assoc($sql))
{
    $regIds[] = $row;
}

mysql_close();

$apiKey = "key";    

// Set POST variables
$urls = 'https://android.googleapis.com/gcm/send';
$fields = array(
           'registration_ids' => $regIds,
             'data' => array( "message" => $message ),
            );
$headers = array(
          'Authorization: key=' . $apiKey,
         'Content-Type: application/json'
          );

//Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $urls );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

//Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
echo $result;
?>  

It doesn't return anything

4
  • That's certainly valid PHP, if that's what you're asking. If you would rather have $regIds merged into the $fields array, you can use array_merge. Commented Aug 11, 2013 at 21:24
  • I think it needs to be like this to use google cloud messaging. Something isn't working, I thought this could be the issue. I'll look elsewhere in the code then, thanks. Commented Aug 11, 2013 at 21:28
  • Are you sure your SQL works? Usually with Joins, you'd need to add the table id like SELECT table1.col1, table2.col1 FROM table1 JOIN table2 .... See docs, Mysql Select or column alias Commented Aug 12, 2013 at 2:51
  • As far as I can tell, everything's OK except that the server host blocked some curl functions. Commented Aug 12, 2013 at 3:22

1 Answer 1

0

That's one way to do it.

When using array_merge, you would lose the $regIds indexes:

$regIds = array( 1, 2, 3, 4 );
$fields = array( 'data' => array( 'message' => $message );
$fields = array_merge( $fields, $regIds );
// How do you know where the $regIds values start? In this case, we know
// they start at 1 (index 0 is the 'data' array)
$fields[index] // Expecting index to be an item on $regIds

While your way is more useful (IMHO):

echo $fields['registration_ids'][0]; // Result: 1

Hope it helps :P.

2
  • Well it's being sent to the google cloud messaging server, so it's supposed to be in a particular format. I'll update the original answer with the complete script. Commented Aug 11, 2013 at 21:49
  • Well, in that case, the documentation says that it must be an array of at least 1 string (with the ID) and up to 1000 IDs, so, you're doing it right -> developer.android.com/google/gcm/gcm.html#send-msg Commented Aug 11, 2013 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.