微信公众号开发第七课 微信实现几个常见功能

微信中可开发功能主要是查询类占多数,对于服务号由于接口更多实现功能也更多,但是个人订阅号能够实现功能就有限的很。下面就提功能几个常见功能函数,都是我网上找的算不上原创,方便大家引用吧。

1.机器人,这个有小黄鸡类似功能

 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.查询图书和电影,这个来自豆瓣api

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.天气查询

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归属地,来自淘宝

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.翻译

//百度翻译
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.手机归属地

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实现一些功能,基本上都是借助这个平台,实际没有多少关于微信的。

原文地址:https://www.cnblogs.com/logoove/p/3413627.html