1

I downloaded a php code from internet for apple push notification at the beginning it is work when I assigned the device token value to the variable direct but when I edited this php code to take the device token value from database MYSQL it did not work :

<?php
// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
$message = 'hi push message';

 ////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS</br>' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

////////////////////////////////////////////////////////////////////////
//make global array
$x = array();

function selectfromdb(){
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("my_db", $con);
 mysql_query("set character_set_server='utf8'");
 mysql_query("set names 'utf8'");
 $result = mysql_query("SELECT idip FROM iphoneid");
$c = 0;
while($r = mysql_fetch_array($result))
{
$x[$c] = $r['idip'];
$c++;
}
}
///////////////////////////////////////////////////////////////////////
selectfromdb();
$i = 0;
while ($i < sizeof($x)){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*',$x[$i]) . pack('n', strlen($payload)) .$payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));


if (!$result)
echo 'Message not delivered</br>' . PHP_EOL;
else
echo 'Message successfully delivered</br>' . PHP_EOL;

$i++;
}
echo 'end</br>';

// Close the connection to the server
fclose($fp);
?>

output : Connected to APNS end

4

1 回答 1

1

看起来你需要添加

global $x;到您的顶部 selectfromdb 函数。不过,让它返回一个值可能会更好。

function selectfromdb(){
    $x = array();
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("my_db", $con);
    mysql_query("set character_set_server='utf8'");
    mysql_query("set names 'utf8'");
    $result = mysql_query("SELECT idip FROM iphoneid");
    $c = 0;
    while($r = mysql_fetch_array($result))
    {
        $x[$c] = $r['idip'];
        $c++;
    }
    return $x;
}
于 2012-07-09T21:59:31.253 回答