2013-01-07 19 views
9

के साथ ऐप्पल पुश अधिसूचना क्या PHP में एक पोस्टिंग पुश अधिसूचना प्रणाली को curl (stream_socket_client के बजाय) का उपयोग करके कार्यान्वित करने का कोई तरीका है?कर्ल

मैं ने लिखा है:

$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 समारोह का उपयोग नहीं करना चाहते हैं, क्योंकि यह एसएसएल प्रोटोकॉल – Pierre

उत्तर

5

डिवाइस टोकन और जेसन संदेश के लिए कोड यहां है, मुझे लगता है कि यह आपकी मदद करेगा।

$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

के साथ प्रॉक्सी विकल्प की अनुमति नहीं देता है काम नहीं करता है और दस्तावेज़ीकरण के अनुसार आपको बाइनरी प्रारूप पूर्ण जेसन की आवश्यकता नहीं है, इसलिए इसे काम नहीं करना चाहिए। यह शहरी एयरशिप पुश एंडपॉइंट के एपीआई की तरह दिखता है। – Zbyszek

+0

यह काम नहीं कर रहा है। कृपया –

+0

को हल करने के लिए मेरी सहायता करें। और आप HTTP के माध्यम से ऐप्पल को पुश अधिसूचनाएं नहीं भेज सकते हैं। आपको वास्तव में टीसीपी बाइनरी संचार का उपयोग करना होगा। –

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

http://stackoverflow.com/questions/13730351/php-https-stream-through-http-proxy देखें यदि आपको stream_context_create के साथ प्रॉक्सी की आवश्यकता है। – Zbyszek