2012-05-18 80 views
0

我正在使用休閒代碼發送推送消息使用PHP這裏是我的代碼。使用PHP發送推送消息時出現401錯誤android設備

<?php 
if($_POST['message']) 
{ 

$username ="email address"; 
$password = "password"; 
$source="Koti-Link-8"; 
$service="ac2dm"; 
$post_params = array ("Email" => $username, "Passwd" => $password, "accountType"=>"HOSTED_OR_GOOGLE", "source" => $source, "service"=>$service); 

$first = true; 
      $data_msg = ""; 

      foreach ($post_params as $key => $value) { 
      if ($first) 
       $first = false; 
      else 
       $data_msg .= "&"; 

      $data_msg .= urlencode($key) ."=". urlencode($value); 
      } 

      $x = curl_init("https://www.google.com/accounts/ClientLogin"); 

      curl_setopt($x, CURLOPT_HEADER, 1); 
      curl_setopt($x, CURLOPT_POST, 1); 
      curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg); 
      curl_setopt($x, CURLOPT_RETURNTRANSFER, 1); 
      $response = curl_exec($x); 
      curl_close($x); 

      echo $response; 
$pos = strpos($response, "Auth="); 
$authKey = trim(substr($response, 5+$pos)); 

$message=$_POST['message']; 


$deviceToken="APA91bEuHNZyyPpczB4NJL-kXitE9-vTti6za3o7x9tA7AjzJIUagHBYteXFUSgcoubrUmg8vVmnLn07XuAzJtgsIs74Q-T33-Vmvo-08C_mh2BMCfr1m5I0rrixT0ymywa3bkqFQEo3KhapTq6Okevvs3ZzoOUGTg"; 
$col_key=date('Y-m-d H:i:s'); 
$data = array(
'registration_id' => $deviceToken, 
'collapse_key' => 'ck_' .$col_key, 
'data.message' => $message, 
'data.title' =>'Requestec Push Demo'); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send"); 
echo 'Content-Length:'.strlen($data); 
    $headers = array('Authorization: GoogleLogin auth=' . $authKey); 
             if($headers){ 
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
             } 
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
             curl_setopt($ch, CURLOPT_POST, true); 
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

             $messagedata = curl_exec($ch); 
             echo $messagedata."<br>"; 
             $success="Sent Successfully"; 
             curl_close($ch); 

} 

?> 

但在使用它顯示了從C2DM服務器401錯誤消息是哪裏的問題以及如何解決it.Please幫我

回答

0

401意思未經授權。未訪問訪客的輸入數據。 您必須檢查發送服務器的所有標題。可能是錯的。

相關問題