php 无法连接到 ssl://gateway.push.apple.com:2195

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3689764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:43:07  来源:igfitidea点击:

unable to connect to ssl://gateway.push.apple.com:2195

phpapple-push-notifications

提问by Praveen kalal

hi i am working on web services using php. in this i need to generate notification on the device using APNS but its gives following error unable to connect to ssl://gateway.push.apple.com:2195

嗨,我正在使用 php 开发 Web 服务。在此我需要使用 APNS 在设备上生成通知,但它给出以下错误无法连接到 ssl://gateway.push.apple.com:2195

my code is following plz help me

我的代码正在关注请帮帮我

<?php
echo '<pre>';
echo 'APNCRON: Started at '.date("Y-m-d H:i:s")."\n";

$apnsHost = 'gateway.push.apple.com'; //gateway.push.apple.com //gateway.sandbox.push.apple.com
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem'; //replace with the appropriate production cert.

//include config.php here
      $con = mysql_connect('localhost','sunil_myapns','fIgBMQ?(G?T_') or die("Could not connect to host.");
      $database = mysql_select_db('sunil_myapns',$con) or die("Could not find database.");    

/**
loop the result 
send payload
*/
/*$sql = 'SELECT p.dev_token, 
        (SELECT COUNT(id) FROM tbl_news s WHERE s.created_date > p.last_access_time) AS cnt
        FROM
        tbl_apn p';*/ //This sql to give you an idea
//echo $sql; 
$sql = 'SELECT * from person';
//echo $sql;
$result = mysql_query($sql);

if(!result)
{
    echo 'APNCRON: Error while executing the query :'.mysql_error();//exit;
}

$rows = mysql_num_rows($result);
//echo $noDev2Push;exit;
if(!$rows)
{
    echo 'APNCRON: No update'; exit;
}

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'tcp', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 120, STREAM_CLIENT_CONNECT, $streamContext);

$i=0;

while($row = mysql_fetch_array($result))
{

    $deviceToken    = 'abcbc12f8d7006be4a544237428f071697bb342608f6408af725ca94ae848ee5'; // remember that devToken is 64 char string
    $cntUpdate = $row['id'];


    $payload = array();
    $payload['aps'] = array(
                                //'alert' => '',    //we are not sending popup alerts 
                                'badge' => (int) $cntUpdate, 
                                'sound' => 'default'
                            );

    $payload = json_encode($payload);
    $i++;
    //echo $payload; echo "\n";

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

    //echo $apnsMessage; echo "\n";

    fwrite($apns, $apnsMessage);
}

fclose($apns);

echo 'APNCRON: Completed at '.date("Y-m-d H:i:s")."\n";
echo 'Sent APN to '.$i.' device(s) ';//.date("Y-m-d H:i:s")."\n";
//socket_close($apns);


?>

thanks in advance

提前致谢

回答by Marc B

I'm able to use telnet to connect to that host:port just fine from my home machine. Most likely something firewalled away that ip/port on your end, or Apple and/or Akamai have blocked you off for some reason.

我可以使用 telnet 从我的家用机器连接到该主机:端口就好了。很可能是因为某些原因,防火墙阻止了您的 IP/端口,或者 Apple 和/或 Akamai 阻止了您。

回答by MkVal

If you're just reusing an old Certificate Signing Request (CSR), make sure to remove the expired/old APNs certificate before exporting the new one and the private key (as p12 file).

如果您只是重复使用旧的证书签名请求 (CSR),请确保在导出新证书和私钥(作为 p12 文件)之前删除过期/旧的 APNs 证书。

回答by Mehul Panchal

Use curl function for access external link and get back response from that

使用 curl 功能访问外部链接并从中获取响应