栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C2DM实现PHP代码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C2DM实现PHP代码

要注册您自己的服务器系统并获取授权令牌(这是Chl。Ohlund提出的建议):

function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") {        session_start();        if( isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null) return $_SESSION['google_auth_id'];        // get an authorization token        $ch = curl_init();        if(!ch){ return false;        }        curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");        $post_fields = "accountType=" . urlenpre('HOSTED_OR_GOOGLE') . "&Email=" . urlenpre($username) . "&Passwd=" . urlenpre($password) . "&source=" . urlenpre($source) . "&service=" . urlenpre($service);        curl_setopt($ch, CURLOPT_HEADER, true);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        // for debugging the request        //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request        $response = curl_exec($ch);        //var_dump(curl_getinfo($ch)); //for debugging the request        //var_dump($response);        curl_close($ch);        if (strpos($response, '200 OK') === false) { return false;        }        // find the auth pre        preg_match("/(Auth=)([w|-]+)/", $response, $matches);        if (!$matches[2]) { return false;        }        $_SESSION['google_auth_id'] = $matches[2];        return $matches[2];    }

要将消息发送到电话:

// $msgType: all messages with same type may be "collapsed": if multiple are sent,// only the last will be received by phone. function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) { $headers = array('Authorization: GoogleLogin auth=' . $authCode); $data = array(     'registration_id' => $deviceRegistrationId,     'collapse_key' => $msgType,     'data.message' => $messageText //TODO Add more params with just simple data instead ); $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, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); curl_close($ch); return $response;        }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号