[php征文]洛洛带你在sae上零基础玩转微信公众平台消息接口 | 张大奋的个人it技术博客
php代码
- <?php
- $poststr = $globals["http_raw_post_data"];
- if (!emptyempty($poststr)){
- $postobj = simplexml_load_string($poststr, ’simplexmlelement’, libxml_nocdata);
- $fromusername = $postobj->fromusername;
- $tousername = $postobj->tousername;
- $keyword = trim($postobj->content);
- $time = time();
- $texttpl = “<xml>
- <tousername><![cdata[%s]]></tousername>
- <fromusername><![cdata[%s]]></fromusername>
- <createtime>%s</createtime>
- <msgtype><![cdata[%s]]></msgtype>
- <content><![cdata[%s]]></content>
- <funcflag>0<funcflag>
- </xml>”;
- if(!emptyempty( $keyword )){
- $msgtype = ”text”;
- $post_data = array();
- $post_data['city'] = $keyword;
- $post_data['submit'] = ”submit”;
- $url=’http://search.weather.com.cn/wap/search.php’;
- $o=”";
- foreach ($post_data as $k=>$v){
- $o.= ”$k=”.urlencode($v).”&”;
- }
- $post_data=substr($o,0,-1);
- $ch = curl_init();
- curl_setopt($ch, curlopt_post, 1);
- curl_setopt($ch, curlopt_header, 0);
- curl_setopt($ch, curlopt_returntransfer, 1);
- curl_setopt($ch, curlopt_url,$url);
- curl_setopt($ch, curlopt_postfields, $post_data);
- $result = curl_exec($ch);
- curl_close($ch);
- $result=explode(‘/’,$result);
- $result=explode(‘.’,$result['5']);
- $citynum = $result['0'];
- $weatherurl = ”http://m.weather.com.cn/data/”.$citynum.”.html”;
- $weatherjson = file_get_contents($weatherurl);
- $weatherarray = json_decode($weatherjson,true);
- $weatherinfo = $weatherarray['weatherinfo'];
- $contenttpl = “#这里是%s#(%s)
- %s%s
- %s时发布的天气预报:
- 今天天气:%s
- %s,%s
- 穿衣指数:%s
- 紫外线指数:%s
- 洗车指数:%s
- 明天天气:%s
- %s,%s
- 后天天气:%s
- %s,%s”;
- $contentstr = sprintf($contenttpl,$weatherinfo['city'],$weatherinfo['city_en'],$weatherinfo['date_y'],$weatherinfo['week'],$weatherinfo['fchh'],$weatherinfo['temp1'],$weatherinfo['weather1'],$weatherinfo['wind1'],$weatherinfo['index_d'],$weatherinfo['index_uv'],$weatherinfo['index_xc'],$weatherinfo['temp2'],$weatherinfo['weather2'],$weatherinfo['wind2'],$weatherinfo['temp3'],$weatherinfo['weather3'],$weatherinfo['wind3']);
- $resultstr = sprintf($texttpl,$fromusername,$tousername,$time,$msgtype,$contentstr);
- echo $resultstr;
- }else{
- echo ”input something…”;
- }
- }else {
- echo ”";
- exit;
- }
- ?>