Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
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. – Otome Aug 11 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. – fonduman Aug 11 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 – Edward Hew Aug 12 at 2:51
As far as I can tell, everything's OK except that the server host blocked some curl functions. – fonduman Aug 12 at 3:22

1 Answer

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.

share|improve this answer
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. – fonduman Aug 11 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 – Nicolas Aug 11 at 22:17
turns out my host has curl_exec and curl_init disabled... – fonduman Aug 12 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.