2015-07-19 58 views
4

我有一對夫婦的PHP文件負責存儲在我的服務器上GCM操作,他們似乎工作得很好,當他們想,但他們往往返回一條錯誤:GCM捲曲操作超時

Curl error: Operation timed out after 0 milliseconds with 0 out of 0 bytes received

這是服務器問題還是我的GCM代碼有問題?以下是我的php文件:

<?php 

$message = urldecode($_POST['message']); 
$order = urldecode($_POST['order']); 
$registrationIDs = urldecode($_POST['registrationIDs']); 
$apiKey = "API_KEY"; 
$tableID = urldecode($_POST['tableID']); 

$url = 'https://android.googleapis.com/gcm/send'; 

$fields = array(
    'registration_ids' => array($registrationIDs), 
    'data' => array(
     'message' => $message, 
     'tableID' => $tableID, 
     'order' => $order 
    ), 
); 

$headers = array(
    'Authorization: key=' . $apiKey, 
    'Content-Type: application/json' 
); 

// Open connection 
$ch = curl_init(); 

// Set the url, number of POST vars, POST data 
curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 



// Execute post 
$result = curl_exec($ch); 
if(curl_errno($ch)) { 
    echo 'Curl error: ' . curl_error($ch); 
} 

// Close connection 
curl_close($ch); 

echo $result; 

?> 
+0

我建議你查看消息的結構,因爲它可能是你的問題。我正在做和你一樣的CURL連接,我沒有問題。 – eloibm

回答

1

我試圖使用您的代碼發送推送通知,我已經實現了它。

For tests I recommend you to set the "dry_run" param. You will be sending messages to GCM and it will return it to you as a "fake" response.

現在你的問題,我已搜查會發生什麼,因爲它似乎你有一個捲曲的限制或東西,但我不是這個主題所以這裏的專家有一些提示,你可以嘗試:

  • 如果您正在運行通過瀏覽器的腳本,然後無限秒的set_time_limit設置爲零。

    set_time_limit(0);

  • 使用此選項 'CURLOPT_TIMEOUT'

    curl_setopt($ch, CURLOPT_TIMEOUT, 20); //20秒

  • 它也可能發生從服務器無窮的重定向增加捲曲的操作時間限制。要停止此操作,請嘗試運行禁用了跟蹤位置的腳本。

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

+1

我會嘗試了這一點感謝..我也沒有捲曲專家,不知道這是怎麼回事,我只是下面的說明再次 – spongyboss

+1

您好,感謝這個,我嘗試過了,它的工作要好得多! :) – spongyboss