GCM curl 操作超时

GCM curl operation time out

我有几个 php 文件负责存储在我的服务器上的 GCM 操作,它们似乎在需要时工作得很好,但它们经常 return 一个错误指出:

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;

?>

我试过使用您的代码发送推送通知,我成功了。

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);

  • 使用此选项增加 curl 的操作时间限制'CURLOPT_TIMEOUT'

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

  • 服务器的无限重定向也可能发生。要停止此操作,请尝试 运行 禁用跟踪位置的脚本。

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);