2011-11-26 12 views
0

C2DMのユーザーになるプッシュ通知に取り組んでいます。私はユーザー登録ID、認証トークンをGoogleから取得できました。しかし、メッセージを送信しようとすると、私はこのエラーをgoogle "411から受け取ります。これはエラーです。POSTリクエストにはContent-lengthヘッダーが必要です"。C2DM 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; 
} 

は、以下しようとしたファイルへのカールのI出力値

()を接続するのに約android.apis.google.comポート443(#0) にするとき私が得る結果であり、 (0) に接続しました。 CAfile:なし CAパス:/ 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 = Googleインターネット機関 SSL証明書の確認がOKです。 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-fo RM-URLエンコード

のContent-Length:207

HTTP 1.0は、終了後に想定し、本体 HTTP/1.0 411の長さは必須

のContent-Type:text/htmlの。文字セット= UTF-8

のContent-Length:11791

日:日、2011年11月27日12時16分06秒GMT

サーバー:GFE/2.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; 

} 
+0

である私はあなたが投稿何をしようとしたが、それはまだ動作していません。同じエラーメッセージ。 –

+0

上記のコードを少し修正してみてください。しかし、問題は、送信しているデータが間違っていることが原因です(未使用の変数 '$ msgType'や' $ messageText'や空の '$ message'変数の使用など)。空の 'data.arg'。 – DaveRandom

+0

すべてのエラーを修正しました。$ messageTextが代わりに使用されました。$ authCodeは機能パラメータから取り除かれましたが、まだ動作しません。 –

0

これに関連したいくつかの事はここにhttp://codershelpingcoders.com.Have

+0

こんにちは、このページはダウンしていますが、上記のエラーと同じですが、解決策について教えてください。 – Chococroc

+0

これはYouTubeのサンプルコードに関するものです。https://github.com/techie28/YouTubeUploadPHPでご覧になれます –

関連する問題