【发布时间】:2012-01-06 23:23:34
【问题描述】:
正在开发一个让 C2DM 用户成为用户的推送通知。我已经能够从 Google 获取用户注册 ID、身份验证令牌。但是当我尝试发送消息时,我从 google 收到此错误“411。这是一个错误。POST 请求需要 Content-length 标头”。
function send($deviceRegistrationId, $msgType, $messageText) {
$f = fopen('request.txt', 'w');
$reg_id = $deviceRegistrationId; // Registration ID
$device_id = "1";
$data = array(
'registration_id' => trim($reg_id),
'collapse_key' => 'ck_'.trim($device_id),
'data.arg' => trim($messageText)
);
$dataStr = http_build_query($data);
$headers = array(
'Authorization: GoogleLogin auth='.$_SESSION['google'],
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($dataStr)
);
// Prepare the cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $f);
// Send the request and echo the response body
$response = curl_exec($ch);
fclose($f);
echo "Reponse is ".$response;
}
以下是我将 curl 的值输出到文件时得到的结果
关于 connect() 到 android.apis.google.com 端口 443 (#0) 正在尝试 74.125.39.139... * 已连接 连接到 android.apis.google.com (74.125.39.139) 端口 443 (#0) 成功设置证书验证位置: CA文件:无 CApath:/etc/ssl/certs 使用 ECDHE-RSA-RC4-SHA 的 SSL 连接 服务器证书: 主题:C=US; ST=加利福尼亚; L=山景; O=谷歌公司; CN=*.google.com 开始日期:格林威治标准时间 2011-11-10 07:48:51 到期日期:格林威治标准时间 2012-11-10 07:58:51 subjectAltName: android.apis.google.com 匹配 发行人:C=US; O=谷歌公司; CN=谷歌互联网管理局 SSL 证书验证正常。 POST /c2dm/发送 HTTP/1.1
主机:android.apis.google.com
接受:/
授权:的GoogleLogin AUTH = DQAAAMIAAADlJmeJmrTjmzdAbt1HiMvvVj_vdSduVXrkEFk_D19OG0o-> FIn1ZzJ25d3MZfDTK2QErF_jEFAndPgC3RoGif6V-gs9w3-FA7VaEWd62qNPnscsqi1j6R0b0J5vtOwGItNmuXm5n1MZrOZ4sd3yx_D95rtzriymmyhilzLWNAyNjPO6FsmX-4Ty_3OwPaw02qe_oHeSvTNt7s6SW-_kT-T1hdJuywCoSf5p2esSzk9sUj9YDwtEXPneDIaB1z2Qy6NcMBjYY8X185GctBttXrjd P>
内容类型:application/x-www-form-urlencoded
内容长度:207
HTTP 1.0,假设在正文之后关闭 HTTP/1.0 411 长度要求
内容类型:文本/html;字符集=UTF-8
内容长度:11791
日期:2011 年 11 月 27 日星期日 12:16:06 GMT
服务器:GFE/2.0
关闭连接 #0
我该如何解决这个问题?
【问题讨论】:
标签: php android android-c2dm