Reputation: 89
I am trying to send a message from a server to client (android) using GCM and curl, the code i used worked perfectly for localhost (emulator and device) , however it would not work for a regular server, attached is the code used and the error message,
are there any additional steps or changes i need to do to make the code work for online server. Any help is appreciated :)
<?php
function Reg($USER_REG_ID)
{
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$response = array();
$API_Key = 'AIzaSyCQgPGhpAzuWGuUd0DCI8pYaXXAItthEsg';
$url = 'https://android.googleapis.com/gcm/send';
$result = mysql_query("SELECT * FROM users where type = 'bus'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$Registration_ID = $row["user_RegID"];
array_push($response, $Registration_ID);
}
//THIS PART SENDS TEH REQUEST USING GCM
$fields = array(
'registration_ids' => $response,
'data' => array(
"message" => $USER_REG_ID
)
);
print_r ($fields);
$headers = array(
'Authorization: key=' . $API_Key,
'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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
}
Reg(1)
?>
the fields array
Array ( [registration_ids] => Array ( [0] => APA91bFolV5ZK2diIbTyxwPLp6BqTMdhqaMDYgEsGOlznmxn4NRV6QQzPtfr58ORrpmxq0N_UMwz6s30gWgj--w5SqHxCEHh662PnvjDpGce_4EiHDe-muflVYMOsLlbVDRybvNDubX9aF_RE4VPJdArUsqQjcrQRg ) [data] => Array ( [message] => 1 ) )
The error message
Curl failed: Failed to connect to 2a00:1450:400c:c05::5f: Network is unreachable
Upvotes: 1
Views: 1235
Reputation: 98901
This should do it:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
If the above doesn't work, you can try to disable ipv6 system wide:
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
Upvotes: 2