2014-01-19 115 views
1

我想使用GCM和curl从服务器发送消息到客户端(android), 我使用的代码完美适用于localhost(模拟器和设备),但它不适用于常规服务器,附上是使用的代码和错误消息,GCM卷曲连接错误

是否有任何额外的步骤或更改我需要做的使代码为联机服务器工作。任何帮助表示赞赏:)

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

回答

2

这应做到:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 

如果上面没有按” t工作,你可以尝试禁用ipv6系统w ide:

echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6 
+0

非常感谢您的回复:D – user2652614

+0

我总是乐意帮忙! –

+0

@ user2652614我正面临这个问题。哪个解决方案适合你。第一个没有帮助,我不知道在哪里使用第二个 – dreamer1989