C2DM Server with PHP 401 Error

大憨熊 提交于 2019-12-06 07:24:25

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.

Try to register your app here. I had the same problem.

I'm not totally sure what's going on here, but here's a few difference I noticed from my application server that works fine.

In your curl_setopt call for CURLOPT_HTTPHEADER there is no space between the content length and the size. This 'shouldn't cause a problem, but I've seen webservers get tempremental over stupid stuff like that before.

Also, CURLOPT_POSTFIELDS is a string whereas I send mine as a key'd array.

Other than that I looks the same as my working code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!