spongyboss
spongyboss

Reputation: 8666

GCM curl operation time out

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;

?>

Upvotes: 4

Views: 1123

Answers (1)

eloibm
eloibm

Reputation: 909

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

Upvotes: 1

Related Questions