2015-10-14 56 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