微信公衆號開發第七課 微信實現幾個常見功能

微信中可開發功能主要是查詢類佔多數,對於服務號因爲接口更多實現功能也更多,可是我的訂閱號可以實現功能就有限的很。下面就提功能幾個常見功能函數,都是我網上找的算不上原創,方便你們引用吧。php

1.機器人,這個有小黃雞相似功能html

 function xiaojo($keyword){

        $curlPost=array("chat"=>$keyword);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');//抓取指定網頁
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 0);//設置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果爲字符串且輸出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//運行curl
        curl_close($ch);
        if(!empty($data)){
            return $data;
        }else{
            $ran=rand(1,5);
            switch($ran){
                case 1:
                    return "小雞雞今天累了,明天再陪你聊天吧。";
                    break;
                case 2:
                    return "小雞雞睡覺嘍~~";
                    break;
                case 3:
                    return "呼呼~~呼呼~~";
                    break;
                case 4:
                    return "你話好多啊,不跟你聊了";
                    break;
                case 5:
                    return "你真的好無聊";
                    break;
                default:
                    return "今天就不聊了";
                    break;
            }
        }
    }

2.查詢圖書和電影,這個來自豆瓣apihtml5

function douban_book($tag='三重門'){
    $str = file_get_contents("http://api.douban.com/v2/book/search?count=1&q=".urlencode($tag));
    $obj = json_decode($str,1);
    if($obj['books']){
    $author =  $obj['books'][0]['author'][0];
    $url=$obj['books'][0]['alt'];
    $con=$obj['books'][0]['summary'];
    $ss = "<a href='$url'>查看$tag</a>--".$author."--".$con;
    return mb_substr($ss,0,600);
    }else{
    return '沒有找到這本書';
    }
}
function douban_film($tag='功夫'){
    $str=file_get_contents("http://api.douban.com/v2/movie/search?count=1&q=".urlencode($tag));
    $obj = json_decode($str,1);
    if($obj['subjects']){
    $str2 = $obj['subjects'][0]['alt'];
    return "<a href='$str2'>查看$tag</a>";
    }else{
    return "沒有找到電影";
    }
}

3.天氣查詢json

function tq($site="深圳"){//支持郵編 區號 地址 
    $key = "http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=".urlencode($site);
    $wether = file_get_contents($key);
    $str =  json_decode($wether,1);
    return $str['text']['content'];
}

4.ip歸屬地,來自淘寶api

function ip($ip=''){
    if(empty($ip)){
        return null;
    }else{
        if(function_exists('file_get_contents')){
    $key =file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);
    }else{
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL,"http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$key = curl_exec($ch);
curl_close($ch);        
    }
    return json_decode($key);
    }        

}

5.身份證微信

function cid($phone=''){
$appkey='10132';
$sign='a9188406bf366b55d58c97b920814f6e';
$s =  file_get_contents("http://api.k780.com/?app=idcard.get&idcard=$phone&appkey=$appkey&sign=$sign&format=json");
$ss = json_decode($s,1);
if($ss['success']==0){
    return $ss["msg"];
}else{
    return $ss['result']['att'].'-'.$ss['result']['sex'];
}
}

6.翻譯app

//百度翻譯
function baiduDic($word,$from="auto",$to="auto"){
        
        //首先對要翻譯的文字進行 urlencode 處理
        $word_code=urlencode($word);
        
        //註冊的API Key
        $appid="PmjnglYuzie9PsMosP45qxyh";
        
        //生成翻譯API的URL GET地址
        $baidu_url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".$appid."&q=".$word_code."&from=".$from."&to=".$to;
        
        $text=json_decode(language_text($baidu_url));

        $text = $text->trans_result;

        return $text[0]->dst;
    }
        
function language_text($url){

        if(!function_exists('file_get_contents')){

            $file_contents = file_get_contents($url);

        }else{
                
            //初始化一個cURL對象
            $ch = curl_init();

            $timeout = 5;

            //設置須要抓取的URL
            curl_setopt ($ch, CURLOPT_URL, $url);

            //設置cURL 參數,要求結果保存到字符串中仍是輸出到屏幕上
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

            //在發起鏈接前等待的時間,若是設置爲0,則無限等待
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

            //運行cURL,請求網頁
            $file_contents = curl_exec($ch);

            //關閉URL請求
            curl_close($ch);
        }

        return $file_contents;
    }

7.手機歸屬地curl

function phone($phone='13823459876'){
$appkey='10132';
$sign='a9188406bf366b55d58c97b920814f6e';
$s =  file_get_contents("http://api.k780.com/?app=phone.get&phone=$phone&appkey=$appkey&sign=$sign&format=json");
$ss = json_decode($s,1);
if($ss['success']==0){
    return $ss["msg"];
}else{
    return $ss["result"]["att"]."-".$ss["result"]["ctype"];
}
}

微信公衆號更多功能都要有特定api實現,對於訂閱號,能實現整合資源實在很少。固然看到有人用html5實現一些功能,基本上都是藉助這個平臺,實際沒有多少關於微信的。函數

相關文章
相關標籤/搜索