【问题标题】:C2DM 411. That’s an error. POST requests require a Content-length headerC2DM 411. 这是一个错误。 POST 请求需要一个 Content-length 标头
【发布时间】: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


    【解决方案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;
    
    }
    

    【讨论】:

    • 我尝试了您发布的内容,但仍然无法正常工作。同样的错误信息。
    • 尝试从上面稍微修正的代码。但是,问题可能是由于您发送的数据不正确(例如未使用的变量$msgType$messageText,或者使用了空的$message 变量,这将导致空的@987654325 @.
    • 我已经改正了所有的错误,改用了$messageText,$authCode 已经从函数参数中取出了,但仍然不起作用
    • 很奇怪 - 我看不出您当前的代码有任何明显的问题。尝试将Connection: close 添加到请求标头中,看看是否可以解决问题。
    • @EdemKofi 在阅读和玩耍之后,我用一个有望工作的新代码编辑了我的答案。让我知道你的进展情况。
    【解决方案2】:

    与此相关的一些东西在这里http://codershelpingcoders.com.Have 看看

    【讨论】:

    • 您好,此页面已关闭,我遇到与上述相同的错误,您能指出解决方案吗?
    • 这是关于 YouTube 示例代码的。你可以在这里找到它github.com/techie28/YouTubeUploadPHP
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2013-04-06
    • 1970-01-01
    • 2011-04-20
    • 2015-03-05
    相关资源
    最近更新 更多