dongxu3029 2012-07-09 21:55
浏览 56
已采纳

推送通知提供程序内置于PHP

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

  • 写回答

1条回答 默认 最新

  • dtrnish3637 2012-07-09 21:59
    关注

    Looks like you need to add

    global $x; to the top of you selectfromdb function. Would probly be better just to have it return a value though.

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置