2013-04-10 105 views
-2

我正在使用Android中的推送通知。在服务器端我实现了这个代码:Curl没有在PHP中给出响应

$apiKey = ""; 


$registrationIDs = array(""); 


$message = "hello"; 


$url = 'https://android.googleapis.com/gcm/send'; 


$fields = array(
       'registration_ids' => $registrationIDs, 
       'data'    => array("message" => $message), 

       ); 


$headers = array( 
        'Authorization: key=' . $apiKey, 
        'Content-Type: application/json' 

       ); 



$ch = curl_init(); 


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_POSTFIELDS, json_encode($fields)); 


$result = curl_exec($ch); 



curl_close($ch); 

echo $result; 

我使用的XAMPP。第一时间,当我运行这段代码的浏览器给出卷曲没有定义的致命错误。然后在php.ini文件中,我取消注释行扩展= p​​hp.curl.dll并重新启动Apache服务器,然后再次运行此代码时,它不会给出任何错误,但不会回显结果。我是新的服务器端任何帮助,将不胜感激

+0

好,愚蠢的问题,你有没有回声/打印的结果呢? – Venu 2013-04-10 05:32:03

+0

好吧,这是愚蠢的问题,因为我没有添加回声$结果看到我编辑的问题。我在做我的回声$结果这就是为什么我问 – User42590 2013-04-10 05:33:50

+3

尝试var_dump而不是回声。失败时可能会返回错误。成功返回TRUE或失败返回FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将成功返回结果,失败时返回FALSE。 – smottt 2013-04-10 05:35:00

回答

2

要获得更多关于错误的信息,您可以使用下面的代码。

$responseInfo = curl_getinfo($ch); 
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
    $responseHeader = substr($result, 0, $header_size); 
    $responseBody = substr($result, $header_size); 

    echo 'Header: <br>'. $responseHeader; 
    echo 'Body: <br>'. $responseBody; 

请检查this

$responseInfo['http_code'] -> gives the http response code. 

或者

如果你不知道如何调试,那么请使用一些库,这将有助于你。 ZendService_Google_Gcm

gcm response codes

快速参考您可以处理这样 https://github.com/zendframework/ZendService_Google_Gcm/blob/master/library/ZendService/Google/Gcm/Client.php#L116

+0

我使用了你的解决方案,但是它给了空头和空var_dump($ result);给布尔(失败) – User42590 2013-04-10 05:57:54

+0

请看我更新的anwser – Venu 2013-04-10 06:08:42

+0

$ responseInfo ['http_code']给0 – User42590 2013-04-10 06:11:07