微信公众平台开发(67)百度百科

微信公众平台开发 微信公众平台开发模式 企业微信公众平台 百度百科 
作者:方倍工作室 
地址:http://www.cnblogs.com/txw1958/p/weixin67-baike.html

 

 

百度百科是一部内容开放、自由的网络百科全书,旨在创造一个涵盖所有领域知识、服务所有互联网用户的中文知识性百科全书。百度百科以平等、协作、分享、自由的互联网精神,提倡网络面前人人平等,所有人共同协作编写百科全书,让知识在一定的技术规则和文化脉络下得以不断组合和拓展。

百度百科

为用户提供一个创造性的网络平台,强调用户的参与和奉献精神,充分调动互联网所有用户的力量,汇聚上亿用户的头脑智慧,积极交流和分享,同时实现与搜索引擎的完美结合,从各个不同层次上满足用户对信息的需求。
百度百科所提供的,是一个互联网所有用户均能平等地浏览、创造、完善内容的平台。所有中文互联网用户在百度百科都能找到自己想要的全面、准确、客观的定义性信息

 

 

 从百度百科抓取内容

<?php

//echo getbaike('马化腾'); //输出 http://baike.baidu.com/view/1466380.htm

function getEncyclopediaInfo($name){
    $name_gbk = iconv('utf-8', 'gbk', $name); //将字符转换成GBK编码,若文件为GBK编码可去掉本行
    $encode = urlencode($name_gbk); //对字符进行URL编码
    $url = 'http://baike.baidu.com/list-php/dispose/searchword.php?word=' .$encode. '&pic=1';
    $get_contents = httpGetRequest_baike($url); //获取跳转页内容
    $get_contents_gbk = iconv('gbk', 'utf-8', $get_contents); //将获取的网页转换成UTF-8编码,若文件为GBK编码可去掉本行
    preg_match("/URL=(\S+)'>/s", $get_contents_gbk, $out); //获取跳转后URL
    $real_link = 'http://baike.baidu.com' .$out[1];

    $get_contents2 = httpGetRequest_baike($real_link); //获取跳转页内容
    preg_match('#"Description"\scontent="(.+?)"\s\/\>#is', $get_contents2, $matchresult);
    if (isset($matchresult[1]) && $matchresult[1] != ""){
        return $matchresult[1];
    }else{
        return "抱歉,没有找到与“".$name."”相关的百科结果。";
    }
}


function httpGetRequest_baike($url)
{
    $headers = array(
        "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: en-us,en;q=0.5",
        "Referer: http://www.baidu.com/"
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $output = curl_exec($ch);
    curl_close($ch);

    if ($output === FALSE){
        return "cURL Error: ". curl_error($ch);
    }
    return $output;
}
?>

 

 

 微信中调用

<?php

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

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

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        logger("R ".$postStr);
        //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->receiveText($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            logger("T ".$resultStr);
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveText($object)
    {
        $funcFlag = 0;
        $keyword = trim($object->Content);
        $resultStr = "";
        $contentStr = "";

        include('baike.php');
        $contentStr = getEncyclopediaInfo($keyword);
        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
        return $resultStr;
    }

    private function receiveEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "欢迎关注";
        }
        $resultStr = $this->transmitText($object, $contentStr);
        return $resultStr;
    }

    private function transmitText($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;
    }

}

function logger($log_content)
{

}
?>

 


欢迎关注方倍工作室微信,体验我们开发的功能及了解行业的最新动态

posted @ 2013-10-07 12:42  方倍工作室  阅读(5669)  评论(6编辑  收藏  举报