dongzhangji4824 2013-07-03 14:18
浏览 100

通过代理向APNS发送推送通知

i've read different question similar to mine, but no answer works for me:

$deviceToken = 'token';

$passphrase = 'passphrase';

$message = 'Questa è una notifica!';

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

$ctx = stream_context_create();
$options = array('http' => array('proxy' => 'tcp://proxy.net:8080', 'request_fulluri' => true));

stream_context_create($options);
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.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);

the error is

Failed to connect: 110 Connection timed out 

i've tried to change $option value with:

$options = array('ssl' => array('proxy' => 'tcp://proxy.net:8080', 'request_fulluri' => true));

but nothing work, the proxy is absolutly minimal and direct connection are not possible.

Any suggestion about something to ask to IT division?

UPDATE

port 2195 is open

  • 写回答

1条回答 默认 最新

  • duanjiao4763 2015-10-06 19:39
    关注

    If your proxy support CONNECT method you need to connect first to the proxy then ask the proxy to connect you to apns server and finally switch up SSL.

    $apns_settings = array(
            "host" => 'gateway.sandbox.push.apple.com',
            "port" => 2195,
            "certificate" => 'ipad.pem',
            "passphrase" => '',
    );
    
    $proxy = 'proxyhttp:8080';
    
    $context_options = array(
            'ssl' => array(
                    'local_cert' => $apns_settings["certificate"],
                    'passphrase' => $apns_settings["passphrase"],
                    'peer_name' => $apns_settings["host"],
                    'SNI_server_name' => $apns_settings["host"],
            ),
    );
    $stream_context = stream_context_create($context_options);
    // connection to your proxy server
    $apns = stream_socket_client('tcp://'.$proxy, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $stream_context);
    // destination host and port must be accepted by proxy
    $connect_via_proxy = "CONNECT ".$apns_settings["host"].":".$apns_settings["port"]." HTTP/1.1
    ".
    "Host: ".$apns_settings["host"].":".$apns_settings["port"]."
    ".
    "User-Agent: SimplePush
    ".
    "Proxy-Connection: Keep-Alive
    
    ";
    fwrite($apns,$connect_via_proxy,strlen($connect_via_proxy));
    // read whole response and check successful "HTTP/1.0 200 Connection established"
    if($response = fread($apns,1024)) {
            $parts = explode(' ',$response);
            if($parts[1] !== '200') { 
                    die('Connection error: '.trim($response));
            } else {
                    echo "R:".$response.":R
    ";
            }
    } else {
            die('Timeout or other error');
    }
    echo "Proxy opened communication...
    ";
    // switch to SSL encrypted communication using local certificate from $context_options
    if (stream_socket_enable_crypto($apns,true,STREAM_CRYPTO_METHOD_TLS_CLIENT))
        echo "Switched to SSL OK...
    ";
    else
        die('some error in SSL negociation');
    

    You can after this use the stream as normal.

    Have a look at this code here: Fork of SimplePush.php

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向