2015-10-14 90 views
0

我想从curl(PHP或Linux)获得Apple推送通知的反馈。 我发现这段代码发送推送通知从Apple推送通知反馈中获取信息

<?php 
$url = 'https://feedback.push.apple.com:2196'; 
$cert = 'Cert.pem'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSLCERT, $cert); 
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["XXXX"], "aps": {"alert": "test message one!"}}'); 
$curl_scraped_page = curl_exec($ch); 
?> 

反馈服务

苹果提供你应该偶尔轮询反馈服务。这将提供以前但不再有效的设备控件列表,例如,如果用户卸载了您的iPhone应用程序。然后,您可以从数据库中删除deviceToken,以免与无效设备进行通信。

我需要一个PHP脚本来从Apple反馈服务获取此列表。

感谢

+0

什么是不工作? –

+0

@BasvanStein此代码没有给我任何结果。我只需要从PHP代码获得苹果的反馈 –

回答

1

你可以使用下面的代码:

<?php 

    $apnsCert = 'Your_Certificate_File.pem'; //Put your Certificate_PATH/Certificate_File.pem Here 

    $streamContext = stream_context_create(); //Creates a stream context 

    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);//Sets option for a stream | For more information please see this page http://php.net/manual/en/function.stream-context-set-option.php 

    stream_context_set_option($streamContext, 'ssl', 'verify_peer', false); //Sets option for a stream 

    $apns = stream_socket_client('ssl://feedback.push.apple.com:2196', $error, 

    $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext); //Open Internet or Unix domain socket connection 

    echo 'error=' . $error . "<br />"; //Show error number 

    echo 'errorString=' . $errorString . "<br />"; //Show error string 

    $result = fread($apns, 38); // Binary-safe file read and store in $result 

    $unpacked = unpack("N1timestamp/n1length/H*devtoken", $result);//Get token from apple APNS 

    echo 'Token is' ;print_r($unpacked); // Show token and can be replace with database update commands 

    fclose($apns); 
+0

你可以对你的代码发表评论吗?尝试通过使用空行来分隔逻辑块,如配置,阅读和打印到页面,使其更具教学性。 – pedromanoel

+0

请立即看评论 谢谢 – MmParvin