简体   繁体   中英

401 error while sending push message using php to android device

I am using fallowing code to send push message using php here is my code.

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

}

?>

But while using it shows 401 error message from c2dm server where is the problem and how can i resolve it.Please help me

401 it mean unauthorized. Entered data of visitor were not taken. You must check all headers, which send the server. May be something wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM