΢ÐŹ«ÖÚƽ̨¿ª·¢£¨Î壩 ÌìÆøÔ¤±¨¹¦ÄÜ¿ª·¢

5年以前  |  阅读数:672 次  |  编程语言:PHP 

Ò»¡¢¼o½e

Ç°Ãae¼¸ÆªÎÄÕ¶Ô΢ÐŹ«ÖÚƽ̨µÄ¿ªÍ¨¼°¼oµ¥Ê¹ÓÃ×oÁ˼oµ¥µÄ½eÉÜ£¬µ«¶¼Ã»ÓÐÉae¼°µ½Êµ¼ÊʹÓÃÖеÄÎÊÌa£¬ÀýÈçÌiÆø²eѯ£¬¹«½»²eѯ£¬¿iµÝ²eѯµÈ¡£½ÓÏÂÀ´µÄ¼¸ÆªÎÄÕ½«¶Ôʵ¼ÊÉu»iÖлa¾­³£Ê¹Óõ½µÄÒ»Ð(C)¹¦ÄܽøÐпª*¢½²½a£¬ÒÔ¹(C)¶ÁÕ߲ο¼¡£

ÕaһƪÎÄÕ½«¶Ô´o¼ÒÿÌi¶¼»a¹ØÐĵÄÌiÆø²eѯ½øÐпª¢£¬ÀýÈ磬Óû§¢ËÍÏuÏ¢ ¡°ËÕÖÝÌiÆø¡±£¬Ôo»a*µ»ØËÕÖÝʵʱÌiÆø×´¿o£¬ÒÔ¼°Î´À´Á½ÌiÉoÖÁδÀ´ÎaÌiµÄÌiÆø×´¿o¡£

¶þ¡¢Ë¼ÂÖÎo**

Ê×ÏÈÒª¶ÔÓû§¢Ë͹ýÀ´µÄÏuÏ¢½øÐÐÅжϣ¬ÅжÏÏuÏ¢ÀiÊÇñº¬ÓС°ÌiÆø¡±¹Ø¼u×Ö£¬Èç¹uº¬ÓУ¬ÔoÐeÒª¼ÌÐøÌaÈ¡µØÇøÐÅÏ¢£¬È»ºoÔÙͨ¹ýÖйuÌiÆøÍø£¨http://www.weather.com.cn£(C)Ìa¹(C)µÄ¿ª*ÅAPI½øÐÐÏa¹ØµØÇøÌiÆø²eѯ¡£

Èý¡¢¹Ø¼u×ÖÅжÏÓeµØÇø¶ÁÈ¡

Óû§¢Ë͹ýÀ´²eѯÌiÆøµÄÏuÏ¢µÄ¸ñʽÊǹ̶¨ºÃµÄ£¬¼´ ¡°µØÇø+ÌiÆø¡±£¬ËuÒÔÊ×ÏȽØÈ¡ºoÁ½¸o×Ö£¬ÅжÏÊÇñΪ ¡°ÌiÆø¡± ¹Ø¼u×Ö¡£

ʹÓÃphpº¯Êý mb_substr() ½ØÈ¡£¬¹ØÓڸú¯ÊýµÄÓÃ*¨£º

mb_substr ¡ª »ñÈ¡×Öu´®µÄ²¿Ö


    ¡¡string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )

˵Ã÷£º¸u¾Ý×ÖuÊýÖ´ÐÐÒ»¸o¶a×Ö½Ú°²È«µÄ substr() ²Ù×÷¡£ λÖÃÊÇ´Ó str µÄ¿ªÊ¼Î»ÖýøÐмÆÊý¡£ µÚÒ»¸o×ÖuµÄλÖÃÊÇ 0¡£µÚ¶þ¸o×Ö*uµÄλÖÃÊÇ 1£¬ÒÔ´ËÀaÍÆ¡£

²ÎÊý£º

str

´Ó¸Ã string ÖÐÌaÈ¡×Ó×Ö*u´®¡£

start

str ÖÐҪʹÓõĵÚÒ»¸o×Ö*uµÄλÖá£

ÕýÊý -> ´Ó×Öu´®¿ªÍÖ¸¶¨Î»ÖÿªÊ¼£»

¸ºÊý -> ´Ó×Ö*u´®½aβָ¶¨Î»ÖÿªÊ¼£»

length

str ÖÐҪʹÓõÄ×i´o×Ö*uÊý¡£

ÕýÊý -> ´Ó start ´¦¿ªÊ¼×i¶a°uÀ¨ length ¸o×Ö*u;

¸ºÊý -> string Ä(C)β´¦µÄ length ¸o×Öu½«»a±»Â(C)µo£¨Èo start ÊǸºÊýÔo´Ó×Öu´®¿ªÍ*ËaÆð£(C)¡£

encoding

encoding ²ÎÊýΪ×Öu±aÂe¡£Èç¹uÊ¡ÂÔ£¬ÔoʹÓÃÄÚ²¿×Öu±aÂe¡£

*µ»ØÖµ£º

mb_substr() º¯Êý¸u¾Ý start ºÍ length ²ÎÊýµ»Ø str ÖÐÖ¸¶¨µÄ²¿Ö¡£


    $str = mb_substr($keyword,-2,2,"UTF-8");

´ÓÏuÏ¢µÄ½aβÊýµÚ¶þ¸o×Öu¿ªÊ¼½ØÈ¡£¬½ØÈ¡Á½¸o×Öu£¬È»ºo¼ÓÒÔÅжÏÊÇ*ñΪ ¡°ÌiÆø¡± ¹Ø¼u×Ö¡£

ÏÂÃae½øÐеØÇøÌaÈ¡£¬»¹ÊÇʹÓà mb_substr() º¯Êý¡£

$str_key = mb_substr($keyword,0,-2,"UTF-8");

´ÓÏuÏ¢µÄ¿ªÍ¿ªÊ¼£¬½ØµoÄ(C)βµÄÁ½¸o×Öu£¨ÌiÆø£(C)£¬¼ÈµÃµØÇø¹Ø¼u×Ö¡£

È»ºo½øÐÐÅжϣ¬¼Ì¶øµ÷Óú¯Êý²eѯÌiÆøÊý¾Ý¡£


    if($str == 'ÌiÆø' && !empty($str_key))
    {
      //µ÷Óú¯Êý²eѯÌiÆøÊý¾Ý
    }

ËÄ¡¢µ÷Óà weather() º¯Êý²eѯ

ÎÒÃÇÕaÀiµ÷ÓõÄÊÇÖйu¹u¼ÒÆøÏo¾ÖÌa¹(C)µÄÌiÆøÔ¤±¨API½Ó¿Ú£¬½Ó¿ÚµØÖ*£ºhttp://m.weather.com.cn/data/101190401.html

URLÖеÄÊý×ÖÖ¸´u³ÇÊеıaºÅ101190401£¨ËÕÖÝ£(C)£¬ÆaËu³ÇÊжÔÓ¦¹Øϵ½«ÔÚÏÂÃaeÌa¹(C)¡£

¸Ã½Ó¿Ú*µ»ØÐÅÏ¢±È½ÏÈ«Ãae£¬Ò²ÊÇÒÔjson¸ñʽÌa¹(C)£¬¸ñʽÈçÏ£º


    {"weatherinfo":{
    //»u±¾ÐÅÏ¢;
    "city":"ËÕÖÝ","city_en":"suzhou",
    "date_y":"2013Äe7ÔÂ9ÈÕ","date":"","week":"ÐÇÆÚ¶þ","fchh":"18","cityid":"101190401",
    //ÉaÊÏζÈ
    "temp1":"30¡ae~37¡ae",
    "temp2":"30¡ae~37¡ae",
    "temp3":"29¡ae~35¡ae",
    "temp4":"27¡ae~33¡ae",
    "temp5":"27¡ae~31¡ae",
    "temp6":"27¡ae~35¡ae",
    //»ªÊÏζÈ;
    "tempF1":"86¨H~98.6¨H",
    "tempF2":"86¨H~98.6¨H",
    "tempF3":"84.2¨H~95¨H",
    "tempF4":"80.6¨H~91.4¨H",
    "tempF5":"80.6¨H~87.8¨H",
    "tempF6":"80.6¨H~95¨H",
    //ÌiÆøÃeÊo;
    "weather1":"Ççת¶aÔÆ",
    "weather2":"Ççת¶aÔÆ",
    "weather3":"Ççת¶aÔÆ",
    "weather4":"¶aÔÆ",
    "weather5":"À×ÕoÓeתÖÐÓe",
    "weather6":"À×ÕoÓeת¶aÔÆ",
    //ÌiÆøÃeÊoͼƬÐoºÅ
    "img1":"0",
    "img2":"1",
    "img3":"0",
    "img4":"1",
    "img5":"0",
    "img6":"1",
    "img7":"1",
    "img8":"99",
    "img9":"4",
    "img10":"8",
    "img11":"4",
    "img12":"1",
    //ͼƬÃu³Æ;
    "img_single":"1",
    "img_title1":"Çç",
    "img_title2":"¶aÔÆ",
    "img_title3":"Çç",
    "img_title4":"¶aÔÆ",
    "img_title5":"Çç",
    "img_title6":"¶aÔÆ",
    "img_title7":"¶aÔÆ",
    "img_title8":"¶aÔÆ",
    "img_title9":"À×ÕoÓe",
    "img_title10":"ÖÐÓe",
    "img_title11":"À×ÕoÓe",
    "img_title12":"¶aÔÆ",
    "img_title_single":"¶aÔÆ",
    //*çËÙÃeÊo
    "wind1":"Î÷ÄÏ*ç3-4¼¶",
    "wind2":"Î÷ÄÏ*ç3-4¼¶",
    "wind3":"¶«ÄÏ*ç3-4¼¶",
    "wind4":"¶«ÄÏ*ç3-4¼¶×ª4-5¼¶",
    "wind5":"¶«ÄÏ*ç4-5¼¶×ªÎ÷ÄÏ*ç3-4¼¶",
    "wind6":"Î÷ÄÏ*ç3-4¼¶×ª4-5¼¶",
    //*çËÙ¼¶±ðÃeÊo
    "fx1":"Î÷ÄÏ*ç",
    "fx2":"Î÷ÄÏ*ç",
    "fl1":"3-4¼¶",
    "fl2":"3-4¼¶",
    "fl3":"3-4¼¶",
    "fl4":"3-4¼¶×ª4-5¼¶",
    "fl5":"4-5¼¶×ª3-4¼¶",
    "fl6":"3-4¼¶×ª4-5¼¶",
    //½ñÈÕ´(C)ÒÂÖ¸Êý;
    "index":"Ñ×ÈÈ",
    "index_d":"ÌiÆøÑ×ÈÈ£¬½¨Òe×ŶÌÉÀ¡¢¶Ìȹ¡¢¶Ì¿a¡¢±¡ÐÍTÐoÉÀµÈÇaÁ¹Ïļ¾*þ×°¡£",
    //48Сʱ´(C)ÒÂÖ¸Êý
    "index48":"Ñ×ÈÈ",
    "index48_d":"ÌiÆøÑ×ÈÈ£¬½¨Òe×ŶÌÉÀ¡¢¶Ìȹ¡¢¶Ì¿a¡¢±¡ÐÍTÐoÉÀµÈÇaÁ¹Ïļ¾*þ×°¡£",
    //×ÏÍaÏß¼°48Сʱ×ÏÍaÏß
    "index_uv":"ÖеÈ",
    "index48_uv":"ÖеÈ",
    //Ï´³µÖ¸Êý
    "index_xc":"ÊÊÒË",
    //ÂÃÓÎÖ¸Êý
    "index_tr":"½Ï²»ÒË",
    //ÊaeÊÊÖ¸Êý
    "index_co":"ºÜ²»ÊaeÊÊ",
    "st1":"36",
    "st2":"28",
    "st3":"36",
    "st4":"28",
    "st5":"34",
    "st6":"27",
    //³¿Á*Ö¸Êý
    "index_cl":"½ÏÊÊÒË",
    //ÁÀɹָÊý
    "index_ls":"ÊÊÒË",
    //¹ýÃoÖ¸Êý
    "index_ag":"²»Ò×*¢"}}

ÎÒÃÇ¿ÉÒÔͨ¹ý½aÎoJSON£¬»ñÈ¡ÏaÓ¦³ÇÊеÄÌiÆøÊý¾Ý¡£

weather() º¯ÊýÈçÏ£º


    private function weather($n){
     include("weather_cityId.php");
     $c_name=$weather_cityId[$n];
     if(!empty($c_name)){
      $json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html");
      return json_decode($json);
     } else {
      return null;
     }
    }

ÕaÀiinclude ÁËÒ»¸o³ÇÊжÔÓ¦¹ØϵÎļþ weather_cityId.php£¬¸ñʽÈçÏ£º


    <?php
    $weather_cityId = array("±±¾(C)"=>"101010100","ÉϺ£"=>"101020100","ËÕÖÝ"=>"101190401");
    ?>

¸u¾Ý´«ÈeµÄ³ÇÊÐÃu£¬µÃµ½³ÇÊдuÂe£¬Èç¹u²»Îª¿Õ£¬Ôoµ÷ÓÃÖйuÌiÆøÍøµÄAPI½øÐвeѯ£¬µ»Øjson¸ñʽµÄÊý¾Ý£¬È»ºo½øÐнaÎo²¢µ»ØÊý¾Ý£¬Èç¹uΪ¿Õ£¬Ôo*µ»ØnullÖµ¡£

Îa¡¢×eÖ¯»Ø¸´ÏuÏ¢ÐÎʽ

Åжϵ»ØÊý¾ÝÊÇñΪ¿Õ£¬ÈoΪ¿Õ£¬Ôo $contentStr = "±§Ç¸£¬Ã»Óвeµ½\"".$str_key."\"µÄÌiÆøÐÅÏ¢£¡";

Èo*µ»ØÊý¾Ý²»Îª¿Õ£¬Ôo£º


    $contentStr = "¡¾".$data->weatherinfo->city."ÌiÆøÔ¤±¨¡¿\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."ʱ*¢²¼"."\n\nʵʱÌiÆø\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\nÎÂÜ°Ìaʾ£º".$data->weatherinfo->index_d."\n\nÃ÷Ìi\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\nºoÌi\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3;

˵Ã÷£º

$data->weatherinfo->city¡¡¡¡//»ñÈ¡³ÇÊÐÃu£¬ÕaÀiΪËÕÖÝ

$data->weatherinfo->date_y¡¡¡¡//»ñÈ¡ÈÕÆÚ£¬ÕaÀiΪ2013Äe7ÔÂ9ÈÕ

$data->weatherinfo->fchh¡¡¡¡//Êý¾Ý*¢²¼Ê±¼a

$data->weatherinfo->weather1¡¡¡¡//ʵʱÌiÆø

$data->weatherinfo->temp1¡¡¡¡//ʵʱζÈ

$data->weatherinfo->wind1¡¡¡¡//ʵʱçÏoºÍçËÙ

$data->weatherinfo->index_d¡¡¡¡//´(C)ÒÂÖ¸Êý

weather2, temp2, wind2 Ö±ð´u±iÁËÃ÷ÌiµÄÌiÆø£¬Î¶ȺÍçÏo*çËÙ£¬ÆaËuµÄÒÔ´ËÀaÍÆ¡£

\n¡¡¡¡//±iʾ»»ÐÐ

Áu¡¢²aÊÔ

Æß¡¢ÍeÕu´uÂe


    <?php
    /**
     * wechat php test
     */

    //define your token
    define("TOKEN", "zhuojin");
    $wechatObj = new wechatCallbackapiTest();
    $wechatObj->responseMsg();
    //$wechatObj->valid();

    class wechatCallbackapiTest
    {
     /*public function valid()
     {
      $echoStr = $_GET["echostr"];

      //valid signature , option
      if($this->checkSignature()){
       echo $echoStr;
       exit;
      }
     }*/

     public function responseMsg()
     {
      //get post data, May be due to the different environments
      $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

       //extract post data
      if (!empty($postStr)){

         $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $RX_TYPE = trim($postObj->MsgType);

        switch($RX_TYPE)
        {
         case "text":
          $resultStr = $this->handleText($postObj);
          break;
         case "event":
          $resultStr = $this->handleEvent($postObj);
          break;
         default:
          $resultStr = "Unknow msg type: ".$RX_TYPE;
          break;
        }
        echo $resultStr;
      }else {
       echo "";
       exit;
      }
     }

     public function handleText($postObj)
     {
      $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(!empty( $keyword ))
      {
       $msgType = "text";

       //ÌiÆø
       $str = mb_substr($keyword,-2,2,"UTF-8");
       $str_key = mb_substr($keyword,0,-2,"UTF-8");
       if($str == 'ÌiÆø' && !empty($str_key)){
        $data = $this->weather($str_key);
        if(empty($data->weatherinfo)){
         $contentStr = "±§Ç¸£¬Ã»Óвeµ½\"".$str_key."\"µÄÌiÆøÐÅÏ¢£¡";
        } else {
         $contentStr = "¡¾".$data->weatherinfo->city."ÌiÆøÔ¤±¨¡¿\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."ʱ*¢²¼"."\n\nʵʱÌiÆø\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\nÎÂÜ°Ìaʾ£º".$data->weatherinfo->index_d."\n\nÃ÷Ìi\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\nºoÌi\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3;
        }
       } else {
        $contentStr = "¸ÐлÄu¹Ø×¢¡¾×¿½oËÕÖÝ¡¿"."\n"."΢Ðźţºzhuojinsz"."\n"."׿Խ½oÐa£¬Ãu³ÇËÕÖÝ£¬ÎÒÃÇΪÄuÌa¹(C)ËÕÖݱ¾µØÉu»iÖ¸ÄÏ£¬ËÕÖÝÏa¹ØÐÅÏ¢²eѯ£¬×o×iºÃµÄËÕÖÝ΢ÐÅƽ̨¡£"."\n"."Ŀǰƽ̨¹¦ÄÜÈçÏ£º"."\n"."¡¾1¡¿ ²eÌiÆø£¬ÈçÊaÈe£ºËÕÖÝÌiÆø"."\n"."¡¾2¡¿ ²e¹«½»£¬ÈçÊaÈe£ºËÕÖݹ«½»178"."\n"."¡¾3¡¿ *­Òe£¬ÈçÊaÈe£º*­ÒeI love you"."\n"."¡¾4¡¿ ËÕÖÝÐÅÏ¢²eѯ£¬ÈçÊaÈe£ºËÕÖݹÛÇ°½Ö"."\n"."¸u¶aÄÚÈÝ£¬¾´ÇeÆÚ´ý...";
       }
       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
       echo $resultStr;
      }else{
       echo "Input something...";
      }
     }

     public function handleEvent($object)
     {
      $contentStr = "";
      switch ($object->Event)
      {
       case "subscribe":
        $contentStr = "¸ÐлÄu¹Ø×¢¡¾×¿½oËÕÖÝ¡¿"."\n"."΢Ðźţºzhuojinsz"."\n"."׿Խ½oÐa£¬Ãu³ÇËÕÖÝ£¬ÎÒÃÇΪÄuÌa¹(C)ËÕÖݱ¾µØÉu»iÖ¸ÄÏ£¬ËÕÖÝÏa¹ØÐÅÏ¢²eѯ£¬×o×iºÃµÄËÕÖÝ΢ÐÅƽ̨¡£"."\n"."Ŀǰƽ̨¹¦ÄÜÈçÏ£º"."\n"."¡¾1¡¿ ²eÌiÆø£¬ÈçÊaÈe£ºËÕÖÝÌiÆø"."\n"."¡¾2¡¿ ²e¹«½»£¬ÈçÊaÈe£ºËÕÖݹ«½»178"."\n"."¡¾3¡¿ *­Òe£¬ÈçÊaÈe£º*­ÒeI love you"."\n"."¡¾4¡¿ ËÕÖÝÐÅÏ¢²eѯ£¬ÈçÊaÈe£ºËÕÖݹÛÇ°½Ö"."\n"."¸u¶aÄÚÈÝ£¬¾´ÇeÆÚ´ý...";
        break;
       default :
        $contentStr = "Unknow Event: ".$object->Event;
        break;
      }
      $resultStr = $this->responseText($object, $contentStr);
      return $resultStr;
     }

     public function responseText($object, $content, $flag=0)
     {
      $textTpl = "<xml>
         <ToUserName><![CDATA[%s]]></ToUserName>
         <FromUserName><![CDATA[%s]]></FromUserName>
         <CreateTime>%s</CreateTime>
         <MsgType><![CDATA[text]]></MsgType>
         <Content><![CDATA[%s]]></Content>
         <FuncFlag>%d</FuncFlag>
         </xml>";
      $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
      return $resultStr;
     }

     private function weather($n){
      include("weather_cityId.php");
      $c_name=$weather_cityId[$n];
      if(!empty($c_name)){
       $json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html");
       return json_decode($json);
      } else {
       return null;
      }
     }

     private 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;
      }
     }
    }

    ?>

¹ØÓÚ³ÇÊжÔÓ¦¹ØϵÎļþ weather_cityId.php£¬Ä¿Ç°¸uе½ÁË2564¸o³ÇÊУ¬ÒÔºo»¹»a¼ÌÐøÔo¼Ó£¬Çeµ½¹Ù*½ÍøÅÌhttp://pan.baidu.com/s/1gfcBX6NÏÂÔØ°É¡£

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例