我正在尝试使用GCM和curl从服务器向客户端(android)发送消息, 我使用的代码完全适用于localhost(模拟器和设备),但它不适用于常规服务器,附带使用的代码和错误消息,
我是否需要做任何其他步骤或更改才能使代码适用于在线服务器。任何帮助表示赞赏:)
<?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)
?>
字段数组
Array ( [registration_ids] => Array ( [0] => APA91bFolV5ZK2diIbTyxwPLp6BqTMdhqaMDYgEsGOlznmxn4NRV6QQzPtfr58ORrpmxq0N_UMwz6s30gWgj--w5SqHxCEHh662PnvjDpGce_4EiHDe-muflVYMOsLlbVDRybvNDubX9aF_RE4VPJdArUsqQjcrQRg ) [data] => Array ( [message] => 1 ) )
错误消息
Curl failed: Failed to connect to 2a00:1450:400c:c05::5f: Network is unreachable
答案 0 :(得分:2)
这应该这样做:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
如果以上操作不起作用,您可以尝试禁用ipv6系统范围:
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6