2013-01-07 14 views
9

cURL(stream_socket_clientではなく)を使用してPHPで実装する方法はありますか?cURLを使用したAppleプッシュ通知

は私が書いた:

$url = 'https://gateway.sandbox.push.apple.com:2195'; 
$cert = 'AppCert.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_scraped_page = curl_exec($ch); 

しかし、どのようにトークンデバイスとJSONメッセージを設定しますか?

+0

なぜ、容易に利用可能なライブラリを見ていない> http://www.easyapns.com/ – BenM

+0

@BenM私はそれのでstream_socket_client機能を使用したくないので、プロキシオプションをsslプロトコルで使用することはできません – Pierre

答えて

5

ここにデバイストークンとjsonメッセージのコードがありますが、これが役立つと思います。

$url = 'https://gateway.sandbox.push.apple.com:2195'; 
$cert = 'AppCert.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": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}'); 

$curl_scraped_page = curl_exec($ch); 
+0

動作しません。ドキュメントによると、完全なjsonではないバイナリ形式が必要なので、動作しません。 Urban AirshipプッシュエンドポイントのAPIのようです。 – Zbyszek

+0

動作していません。それを解決するために私を助けてください –

+0

働いていません。そして、HTTP経由でAppleにプッシュ通知を送ることはできません。 TCPバイナリ通信を使用する必要があります。 –

4

このコードが動作する

$deviceToken = '...'; 
$passphrase = '...'; 
$message = '...'; 
$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 
$body['aps'] = array('alert' => $message,'sound' => 'default'); 
$payload = json_encode($body); 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
$result = fwrite($fp, $msg, strlen($msg)); 
fclose($fp); 
+0

これは、ありがとう! –

+0

stream_context_createでプロキシが必要な場合は、http://stackoverflow.com/questions/13730351/php-https-stream-through-http-proxyを参照してください。 – Zbyszek