0

我正在使用休闲代码使用 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 错误消息问题出在哪里以及如何解决它。请帮助我

4

1 回答 1

0

401 表示未经授权。访问者的输入数据未被获取。您必须检查发送服务器的所有标头。可能有问题。

于 2012-05-18T11:44:05.703 回答