GCM curl operation time out

痴心易碎 提交于 2019-12-21 17:28:26

问题


I have a couple of php files responsible for GCM operations stored on my server, they seem to work just fine when they want to but they often return an error that states:

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

Is this is a problem with the server or a problem with my GCM code? Below is my php file:

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

?>

回答1:


I have tried to send a Push notification using your code and I have achieved it.

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.

Now your problem, I have searched what could happen because it seems that you have a curl limitation or something but I am not expert of this theme so here are some tips that you can try:

  • If you are running the script through browser, then set the set_time_limit to zero for infinite seconds.

    set_time_limit(0);

  • Increase the curl's operation time limit using this option 'CURLOPT_TIMEOUT'

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

  • It can also happen for infinite redirection from the server. To halt this, try to run the script with follow location disabled.

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);



来源:https://stackoverflow.com/questions/31502664/gcm-curl-operation-time-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!