本文实例为大家分享了php微信百度天气预报的开发代码,供大家参考,具体内容如下

1.登录百度ak申请:http://lbsyun.baidu.com/apiconsole/key

php微信开发之百度天气预报

2.实现天气信息功能

baiduWeather.php 

<"" || (strstr($cityName,"+"))){ 
   return "发送城市加天气,例如北京天气"; 
  } 
  //获取到的ak 
  $ak = your ak; 
  //获取到的sk 
  $sk = your sk; 
  //调用接口 
  $url = 'http://api.map.baidu.com/telematics/v3/weather"error"]!=0){ 
   return $result["status"]; 
  } 
 
  $curHour = (int)date('H',time()); 
  $weather = $result["results"][0]; 
  $weatherArray[]=array("Title"=>$weather['currentCity']."天气预报","Description"=>"","PicUrl"=>"","Url"=>""); 
  for($i = 0;$i<count($weather["weather_data"]);$i++){ 
   $weatherArray[] = array("Title"=> 
    $weather["weather_data"][$i]["data"]."\n". 
    $weather["weather_data"][$i]["weather"]. 
    $weather["weather_data"][$i]["wind"]. 
    $weather["weather_data"][$i]["temperature"], 
    "Description"=>"", 
    "PicUrl"=>(($curHour>=6)&&($curHour< 
    18))"weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"],"URL"=>"" 
   ); 
  } 
  return $weatherArray; 
 } 

3.实现天气消息事件

<"TOKEN", "weixin"); 
/** 
 * 百度天气预报案例实现 
 * 实现思路: 
 * 1.申请百度ak、sk 
 * 2.使用百度天气预报接口 
 * 3.实现天气信息功能 
 * 4.实现事件响应功能 
 */ 
$wechatObj = new wechatCallbackapiTest(); 
if (!isset($_GET['echostr'])) { 
 $wechatObj->responseMsg(); 
}else{ 
 $wechatObj->valid(); 
} 
 
class wechatCallbackapiTest 
{ 
 //验证签名 
 public function valid() 
 { 
  $echoStr = $_GET["echostr"]; 
  if($this->checkSignature()){ 
   header('content-type:text'); 
   echo $echoStr; 
   exit; 
  } 
 } 
 
 public function checkSignature(){ 
  $signature = $_GET["signature"]; 
  $timestamp = $_GET["timestamp"]; 
  $nonce = $_GET["nonce"]; 
  $token = TOKEN; 
  $tmpArr = array($token, $timestamp, $nonce); 
  sort($tmpArr); 
  $tmpStr = implode($tmpArr); 
  $tmpStr = sha1($tmpStr); 
  if($tmpStr == $signature) { 
   return true; 
  }else{ 
   return false; 
  } 
 } 
 
 //响应消息 
 public function responseMsg() 
 { 
  $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  if (!empty($postStr)){ 
   $this->logger("R ".$postStr); 
   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
   $RX_TYPE = trim($postObj->MsgType); 
 
   //消息类型分离 
   switch ($RX_TYPE) 
   { 
    case "event": 
     $result = $this->receiveEvent($postObj); 
     break; 
    case "text": 
     $result = $this->receiveText($postObj); 
     break; 
    default: 
     $result = "unknown msg type: ".$RX_TYPE; 
     break; 
   } 
   echo $result; 
  }else { 
   echo ""; 
   exit; 
  } 
 } 
 
 //接收事件消息 
 public function receiveEvent($object) 
 { 
  $content = ""; 
  switch ($object->Event) 
  { 
   case "subscribe": 
    $content = "欢迎关注Nicky的公众号 "; 
    $content .= (!empty($object->EventKey))"\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):""; 
    break; 
   case "unsubscribe": 
    $content = "取消关注"; 
    break; 
  } 
  $result = $this->transmitText($object, $content); 
  return $result; 
 } 
 
 //接收文本消息 
 public function receiveText($object) 
 { 
  $keyword = trim($object->Content); 
 
  //自动回复模式 
 
  if (strstr($keyword, "天气")){ 
   $city = str_replace('天气','',$keyword); 
   include("baiduweather.php"); 
   $content = getWeatherInfo($city); 
  } 
  $result = $this->transmitNews($object, $content); 
  return $result; 
 } 
 
 //回复图文消息 
 public function transmitNews($object, $newsArray) 
 { 
  if(!is_array($newsArray)){ 
   return; 
  } 
  $itemTpl = " <item> 
  <Title><![CDATA[%s]]></Title> 
  <Description><![CDATA[%s]]></Description> 
  <PicUrl><![CDATA[%s]]></PicUrl> 
  <Url><![CDATA[%s]]></Url> 
 </item> 
"; 
  $item_str = ""; 
  foreach ($newsArray as $item){ 
   $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); 
  } 
  $xmlTpl = "<xml> 
<ToUserName><![CDATA[%s]]></ToUserName> 
<FromUserName><![CDATA[%s]]></FromUserName> 
<CreateTime>%s</CreateTime> 
<MsgType><![CDATA[news]]></MsgType> 
<ArticleCount>%s</ArticleCount> 
<Articles> 
$item_str</Articles> 
</xml>"; 
 
  $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); 
  return $result; 
 } 
 
 
 //日志记录 
 public function logger($log_content) 
 { 
  if(isset($_SERVER['HTTP_APPNAME'])){ //SAE 
   sae_set_display_errors(false); 
   sae_debug($log_content); 
   sae_set_display_errors(true); 
  }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL 
   $max_size = 10000; 
   $log_filename = "log.xml"; 
   if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} 
   file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); 
  } 
 } 
 
} 
"text-align: center">php微信开发之百度天气预报

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
php微信百度天气预报,php微信天气预报,php微信查询天气预报,php微信开发天气预报

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“php微信开发之百度天气预报”

暂无“php微信开发之百度天气预报”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。