C2DM服务器用PHP 401错误 [英] C2DM Server with PHP 401 Error
问题描述
我现在建设自己的C2DM应用。我第一次开始用小的Android应用程序来测试推送功能。和它的作品,如果我只是打电话与我的壳进行正确的设置curl命令。
现在的服务器部分我想用PHP,但因为它似乎我做错了什么,我总是得到一个401错误消息时,我尝试将消息发送到客户端。首先所有的code的由两部分组成。第一卷曲请求要求服务器令牌。这工作我从谷歌有合作的令牌一个真实的反应!
第二个卷曲请求401错误消息结束。任何想法我做错了吗?
$ post_params =阵列(电子邮件=> $ MY_GOOGLE_ACC的passwd=> $ MY_GOOGLE_PWD,ACCOUNTTYPE=>中GOOGLE,源=。 $ MY_GOOGLE_SRC的服务= ac2dm); $第一= TRUE;
$ data_msg =; 的foreach($ post_params为$关键=> $值){
如果(第一$)
$第一= FALSE;
其他
$ data_msg =与&。; $ data_msg = urlen code($键)。=。 urlen code($值);
} $ 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);
$数据= curl_exec($ X);
curl_close($ X);
$响应= $的数据; $ AUTHKEY = TRIM(SUBSTR($响应,4 + strpos($回应,SID =))); 回声$ AUTHKEY;
$ collapse_key的='东西'; $ post_params =阵列(registration_id=> $ DEVICE_TOKENcollapse_key的=> $ collapse_key的,data.payload=>中cakephp的); $第一= TRUE;
$ data_msg =; 的foreach($ post_params为$关键=> $值){
如果(第一$)
$第一= FALSE;
其他
$ data_msg =与&。; $ data_msg = urlen code($键)。=。 urlen code($值);
} $大小= strlen的($ data_msg);
$ X = curl_init(https://android.apis.google.com/c2dm/send);
curl_setopt($ X,CURLOPT_HTTPHEADER,阵列('内容类型:应用程序/ x-WWW的形式urlen codeD,内容长度:'。$大小,'授权:的GoogleLogin AUTH ='$ AUTHKEY) );
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);
$数据= curl_exec($ X);
curl_close($ X);
$响应= $的数据;
key'd阵列的例子卷曲使用。这是pretty多少确切的code我有工作(为了清楚起见,微小的变化)。
$头=阵列('授权:AUTH的GoogleLogin ='$ AUTH code);
$数据=阵列(
registration_id'=> $ device_registration_id,
collapse_key的'=> ck_。 $ DEVICE_ID,
data.arg'=> arrrrghhh
);$ CH = curl_init();curl_setopt($ CH,CURLOPT_URLhttps://android.apis.google.com/c2dm/send);
如果($头)curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
curl_setopt($ CH,CURLOPT_POSTFIELDS,$数据);curl_exec($ CH);
$ AUTH code是通过ClientLogin的返回的SID。 $ device_registration_id是在手机上的客户端应用程序给我们的时候,我们做了C2DM_REGISTER的注册ID。
希望有所帮助。
I'm building my own C2DM application right now. I first started with a small Android application to test the push feature. And it works if I just call the curl command with the correct settings in my shell.
Now for the server part I wanted to use PHP but as it seems I'm doing something wrong as I always get a 401 error message when I try to send a message to the client. First of all the code consists of two parts. The first curl request asks for the server token. This works I get a real response from google with a working token!
The second curl request ends up with a 401 error message. Any ideas what I'm doing wrong?
$post_params = array ( "Email" => $MY_GOOGLE_ACC, "Passwd" => $MY_GOOGLE_PWD, "accountType"=>"GOOGLE", "source=" . $MY_GOOGLE_SRC, "service=ac2dm" );
$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);
$data = curl_exec($x);
curl_close($x);
$response = $data;
$authKey = trim(substr($response, 4+strpos($response, "SID=")));
echo $authKey;
$collapse_key = 'something';
$post_params = array ( "registration_id" => $DEVICE_TOKEN, "collapse_key" => $collapse_key, "data.payload"=>"cakephp" );
$first = true;
$data_msg = "";
foreach ($post_params as $key => $value) {
if ($first)
$first = false;
else
$data_msg .= "&";
$data_msg .= urlencode($key) ."=". urlencode($value);
}
$size=strlen($data_msg);
$x = curl_init("https://android.apis.google.com/c2dm/send");
curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Content-Length:'. $size, 'Authorization: GoogleLogin auth=' . $authKey));
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);
$data = curl_exec($x);
curl_close($x);
$response = $data;
Example of key'd array use with curl. This is pretty much the exact code I have working (with minor changes for clarity).
$headers = array('Authorization: GoogleLogin auth=' . $authcode);
$data = array(
'registration_id' => $device_registration_id,
'collapse_key' => 'ck_' . $device_id,
'data.arg' => 'arrrrghhh'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
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);
curl_exec($ch);
$authcode is the SID returned by ClientLogin. $device_registration_id is the registration ID that the client app on the phone gave us when we did the C2DM_REGISTER.
Hope that helps.
这篇关于C2DM服务器用PHP 401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!