[php征文]洛洛带你在sae上零基础玩转微信公众平台消息接口 | 张大奋的个人it技术博客

我是创始人李岩:很抱歉!给自己产品做个广告,点击进来看看。  
php代码 
  1. <?php
  2. $poststr = $globals["http_raw_post_data"];
  3. if (!emptyempty($poststr)){
  4.     $postobj = simplexml_load_string($poststr, ’simplexmlelement’, libxml_nocdata);
  5.     $fromusername = $postobj->fromusername;
  6.     $tousername = $postobj->tousername;
  7.     $keyword = trim($postobj->content);
  8.     $time = time();
  9.     $texttpl = “<xml>
  10.             <tousername><![cdata[%s]]></tousername>
  11.             <fromusername><![cdata[%s]]></fromusername>
  12.             <createtime>%s</createtime>
  13.             <msgtype><![cdata[%s]]></msgtype>
  14.             <content><![cdata[%s]]></content>
  15.             <funcflag>0<funcflag>
  16.             </xml>”;
  17.     if(!emptyempty( $keyword )){
  18.       $msgtype = ”text”;
  19.       $post_data = array();
  20.       $post_data['city'] = $keyword;
  21.       $post_data['submit'] = ”submit”;
  22.       $url=’http://search.weather.com.cn/wap/search.php’;
  23.       $o=”";
  24.       foreach ($post_data as $k=>$v){
  25.         $o.= ”$k=”.urlencode($v).”&”;
  26.       }
  27.       $post_data=substr($o,0,-1);
  28.       $ch = curl_init();
  29.       curl_setopt($ch, curlopt_post, 1);
  30.       curl_setopt($ch, curlopt_header, 0);
  31.       curl_setopt($ch, curlopt_returntransfer, 1);
  32.       curl_setopt($ch, curlopt_url,$url);
  33.       curl_setopt($ch, curlopt_postfields, $post_data);
  34.       $result = curl_exec($ch);
  35.       curl_close($ch);
  36.       $result=explode(‘/’,$result);
  37.       $result=explode(‘.’,$result['5']);
  38.       $citynum = $result['0'];
  39.       $weatherurl = ”http://m.weather.com.cn/data/”.$citynum.”.html”;
  40.       $weatherjson = file_get_contents($weatherurl);
  41.       $weatherarray = json_decode($weatherjson,true);
  42.       $weatherinfo = $weatherarray['weatherinfo'];
  43.       $contenttpl = “#这里是%s#(%s)
  44. %s%s
  45. %s时发布的天气预报:
  46. 今天天气:%s
  47. %s,%s
  48. 穿衣指数:%s
  49. 紫外线指数:%s
  50. 洗车指数:%s
  51. 明天天气:%s
  52. %s,%s
  53. 后天天气:%s
  54. %s,%s”;
  55.       $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']);
  56.       $resultstr = sprintf($texttpl,$fromusername,$tousername,$time,$msgtype,$contentstr);
  57.       echo $resultstr;
  58.     }else{
  59.       echo ”input something…”;
  60.     }
  61. }else {
  62.     echo ”";
  63.     exit;
  64. }
  65. ?>

随意打赏

提交建议
微信扫一扫,分享给好友吧。