我見過幾篇關於如何從我的PHP服務器發送GCM消息的文章,但是我無法正常工作。這是我的代碼:使用GCM與PHP
public function test_gcm($id_user){
// Search user's RegIds and stores them in $regids
if(count($regids) == 0){
echo "This user has no registered device.";
return;
}
$ch = curl_init();
$data = array(
'data' => array('message'=>'my message', 'title'=>'message title'),
'registration_ids' => $regids
);
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . $apiKey)
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
我正在使用瀏覽器密鑰。我也嘗試了服務器密鑰,但都沒有工作,curl_exec總是返回false。有人知道爲什麼嗎?
編輯:我只是用'netstat -tuanc | grep 173'在我的服務器上,並執行服務器調用。我使用的grep 173,因爲如果我ping android.googleapis.com我ping這個IP地址。當我使用curl_exec時,netstat沒有顯示任何與該ip地址的連接。這是否意味着我沒有連接到android.googleapis.com?或者我在做什麼是錯的?
謝謝!
我得到它的工作是這樣的:http://stackoverflow.com/questions/11396177/gcm-sending-with-curl-php 希望它有幫助 – Klaasvaak
另外:我讓它允許來自所有IP的呼叫。首先測試它。我也使用了瀏覽器鍵。 – Klaasvaak
你是我檢查的帖子之一。我修改了這個命令,讓它和你完全一樣,但它仍然沒有啓動。我有「允許任何IP」服務器密鑰和「允許任何引用者」瀏覽器密鑰。 –