使用 PHP 向 Android 设备发送推送消息时出现 401 错误


401 error while sending push message using php to android device

我正在使用休耕代码使用 php 发送推送消息,这是我的代码。

<?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错误消息,问题在哪里以及如何解决。请帮助我

401 表示未经授权。未采集访客的输入数据。您必须检查发送服务器的所有标头。可能是出了什么问题。