改进用于向Android设备发送通知的php页面

It Send Notification to android devices registered in the mysql database. this is the php code i'm using. and it's working well as far as i can tell.

<?php
$con = mysql_connect("*******.*****.com","*******","*********");

mysql_select_db("*******",$con);

$result1 = mysql_query("SELECT Device_ID FROM Registered_Users") or die(mysql_error());

$api_key = "***************-***********";

  $response["products"] = array(); 
   while ($row = mysql_fetch_array($result1))
 {
     $reg_id = array($row['Device_ID']);
     $registrationIDs = $reg_id;
     $message = $_POST['msg'];

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids'  => $registrationIDs,'data' 
                       => array( "message" => $message ),);
                   $headers = array('Authorization: key=' 
                     . $api_key,'Content-Type: application/json');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    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 ) );
    $result = curl_exec($ch);
    curl_close($ch);
}
echo "success";
?>

my question is : how can i improve this code to get the best performance " results " possible ?

use this way........

<?php
$con = mysql_connect("*******.*****.com","*******","*********");

mysql_select_db("*******",$con);

$result1 = mysql_query("SELECT Device_ID FROM Registered_Users") or die(mysql_error());
while ($row = mysql_fetch_array($result1))
 {
     $reg_id[] = $row['Device_ID'];
     //from this way you dont need to run function again and again......
 }
$api_key = "***************-***********";

  $response["products"] = array(); 

     $registrationIDs = $reg_id;
     $message = $_POST['msg'];

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids'  => $registrationIDs,'data' 
                       => array( "message" => $message ),);
                   $headers = array('Authorization: key=' 
                     . $api_key,'Content-Type: application/json');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    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 ) );
    $result = curl_exec($ch);
    curl_close($ch);

echo "success";
?>

this will increase code performance....may this will help you

You should send one curl request which includes an array of registration_ids. Doing this will use less resources as it only has to connect to Google's servers in one step rather than sending off several requests. You can just send one request to handle many notifications.

You should also be using something other than mysql_* to interact with your database, for example PDO:

<?php
$dbName     = '*****';
$dbUsername = '*****';
$dbPassword = '*****';
$api_key    = "***************-***********";
$url        = 'https://android.googleapis.com/gcm/send';
$message    = $_POST['msg'];

$pdo = new PDO('mysql:host=localhost;dbname=' . $dbName, $dbUsername, $dbPassword);

$stmt      = $pdo->query("SELECT Device_ID FROM Registered_Users");
$dbResults = $stmt->fetchAll();
$stmt->closeCursor();

$regIds = array_map(function (array $dbResult) {
    return $dbResult['Device_ID'];
}, $dbResults);

$fields  = array(
    'registration_ids' => $regIds,
    'data'             => array( "message" => $message ),
);
$headers = array(
    'Authorization: key=' . $api_key,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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));
$result = curl_exec($ch);
curl_close($ch);

echo "success";