2013-04-10 63 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