正在处理使用C2DM用户的推送通知。我已经能够从Google获取用户注册ID,身份验证令牌。但是,当我尝试发送消息时,我从谷歌“411收到此错误。这是一个错误.POST请求需要内容长度标题”。
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) 成功设置证书验证位置: CAfile:无 CApath:/ etc / ssl / certs 使用ECDHE-RSA-RC4-SHA进行SSL连接 服务器证书: 主题:C = US; ST =加利福尼亚; L =山景; O = Google Inc; CN = *。google.com 开始日期:2011-11-10 07:48:51 GMT 到期日期:2012-11-10 07:58:51 GMT subjectAltName:android.apis.google.com匹配 发行人:C = US; O = Google Inc; 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
内容类型:application / x-www-form-urlencoded
内容长度:207
HTTP 1.0,假设身体后关闭 HTTP / 1.0 411所需长度
Content-Type:text / html;字符集= UTF-8
内容长度:11791
日期:太阳,2011年11月27日12:16:06 GMT
服务器:GFE / 2.0
关闭连接#0
我该如何解决这个问题?
答案 0 :(得分:1)
function send ($deviceRegistrationId, $msgType, $messageText) {
$reg_id = $deviceRegistrationId; // Registration ID
$device_id = "1";
// Build request body
$data = array (
'registration_id' => trim($reg_id),
'collapse_key' => 'ck_'.trim($device_id),
'data.arg' => trim($messageText)
);
$dataStr = trim(http_build_query($data));
// Headers for the request
$headers = array(
'Authorization: GoogleLogin auth='.trim($_SESSION['google']),
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.trim(strlen($dataStr)),
'Connection: close'
);
// Prepare the cURL request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://android.apis.google.com/c2dm/send",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $dataStr,
CURLOPT_RETURNTRANSFER => true
));
// For debugging
//$f = fopen('request.txt', 'w');
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_HEADER, 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;
}
答案 1 :(得分:0)
与此相关的一件事是http://codershelpingcoders.com.Have一看