Rumah > Artikel > pembangunan bahagian belakang > 微信公众平台开发(67)百度百科_PHP教程
微信公众平台开发 微信公众平台开发模式 企业微信公众平台 百度百科
作者:方倍工作室
地址:http://www.cnblogs.com/txw1958/p/weixin67-baike.html
百度百科是一部内容开放、自由的网络百科全书,旨在创造一个涵盖所有领域知识、服务所有互联网用户的中文知识性百科全书。百度百科以平等、协作、分享、自由的互联网精神,提倡网络面前人人平等,所有人共同协作编写百科全书,让知识在一定的技术规则和文化脉络下得以不断组合和拓展。
百度百科
为用户提供一个创造性的网络平台,强调用户的参与和奉献精神,充分调动互联网所有用户的力量,汇聚上亿用户的头脑智慧,积极交流和分享,同时实现与搜索引擎的完美结合,从各个不同层次上满足用户对信息的需求。 百度百科所提供的,是一个互联网所有用户均能平等地浏览、创造、完善内容的平台。所有中文互联网用户在百度百科都能找到自己想要的全面、准确、客观的定义性信息
从百度百科抓取内容
<?<span php </span><span //</span><span echo getbaike('马化腾'); //输出 http://baike.baidu.com/view/1466380.htm</span> <span function</span> getEncyclopediaInfo(<span $name</span><span ){ </span><span $name_gbk</span> = <span iconv</span>('utf-8', 'gbk', <span $name</span>); <span //</span><span 将字符转换成GBK编码,若文件为GBK编码可去掉本行</span> <span $encode</span> = <span urlencode</span>(<span $name_gbk</span>); <span //</span><span 对字符进行URL编码</span> <span $url</span> = 'http://baike.baidu.com/list-php/dispose/searchword.php?word=' .<span $encode</span>. '&pic=1'<span ; </span><span $get_contents</span> = httpGetRequest_baike(<span $url</span>); <span //</span><span 获取跳转页内容</span> <span $get_contents_gbk</span> = <span iconv</span>('gbk', 'utf-8', <span $get_contents</span>); <span //</span><span 将获取的网页转换成UTF-8编码,若文件为GBK编码可去掉本行</span> <span preg_match</span>("/URL=(\S+)'>/s", <span $get_contents_gbk</span>, <span $out</span>); <span //</span><span 获取跳转后URL</span> <span $real_link</span> = 'http://baike.baidu.com' .<span $out</span>[1<span ]; </span><span $get_contents2</span> = httpGetRequest_baike(<span $real_link</span>); <span //</span><span 获取跳转页内容</span> <span preg_match</span>('#"Description"\scontent="(.+?)"\s\/\>#is', <span $get_contents2</span>, <span $matchresult</span><span ); </span><span if</span> (<span isset</span>(<span $matchresult</span>[1]) && <span $matchresult</span>[1] != ""<span ){ </span><span return</span> <span $matchresult</span>[1<span ]; }</span><span else</span><span { </span><span return</span> "抱歉,没有找到与“".<span $name</span>."”相关的百科结果。"<span ; } } </span><span function</span> httpGetRequest_baike(<span $url</span><span ) { </span><span $headers</span> = <span array</span><span ( </span>"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/"<span ); </span><span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_URL, <span $url</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span ); curl_setopt(</span><span $ch</span>, CURLOPT_HTTPHEADER, <span $headers</span><span ); </span><span $output</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span><span ); </span><span if</span> (<span $output</span> === <span FALSE</span><span ){ </span><span return</span> "cURL Error: ". curl_error(<span $ch</span><span ); } </span><span return</span> <span $output</span><span ; } </span>?>
微信中调用
<?<span php </span><span define</span>("TOKEN", "weixin"<span ); </span><span $wechatObj</span> = <span new</span><span wechatCallbackapiTest(); </span><span $wechatObj</span>-><span responseMsg(); </span><span class</span><span wechatCallbackapiTest { </span><span public</span> <span function</span><span valid() { </span><span $echoStr</span> = <span $_GET</span>["echostr"<span ]; </span><span //</span><span valid signature , option</span> <span if</span>(<span $this</span>-><span checkSignature()){ </span><span echo</span> <span $echoStr</span><span ; </span><span exit</span><span ; } } </span><span private</span> <span function</span><span checkSignature() { </span><span $signature</span> = <span $_GET</span>["signature"<span ]; </span><span $timestamp</span> = <span $_GET</span>["timestamp"<span ]; </span><span $nonce</span> = <span $_GET</span>["nonce"<span ]; </span><span $token</span> =<span TOKEN; </span><span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span ); </span><span sort</span>(<span $tmpArr</span><span ); </span><span $tmpStr</span> = <span implode</span>( <span $tmpArr</span><span ); </span><span $tmpStr</span> = <span sha1</span>( <span $tmpStr</span><span ); </span><span if</span>( <span $tmpStr</span> == <span $signature</span><span ){ </span><span return</span> <span true</span><span ; }</span><span else</span><span { </span><span return</span> <span false</span><span ; } } </span><span public</span> <span function</span><span responseMsg() { </span><span //</span><span get post data, May be due to the different environments</span> <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ]; logger(</span>"R ".<span $postStr</span><span ); </span><span //</span><span extract post data</span> <span if</span> (!<span empty</span>(<span $postStr</span><span )){ </span><span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span LIBXML_NOCDATA); </span><span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType); </span><span switch</span> (<span $RX_TYPE</span><span ) { </span><span case</span> "text": <span $resultStr</span> = <span $this</span>->receiveText(<span $postObj</span><span ); </span><span break</span><span ; </span><span case</span> "event": <span $resultStr</span> = <span $this</span>->receiveEvent(<span $postObj</span><span ); </span><span break</span><span ; </span><span default</span>: <span $resultStr</span> = "unknow msg type: ".<span $RX_TYPE</span><span ; </span><span break</span><span ; } logger(</span>"T ".<span $resultStr</span><span ); </span><span echo</span> <span $resultStr</span><span ; }</span><span else</span><span { </span><span echo</span> ""<span ; </span><span exit</span><span ; } } </span><span private</span> <span function</span> receiveText(<span $object</span><span ) { </span><span $funcFlag</span> = 0<span ; </span><span $keyword</span> = <span trim</span>(<span $object</span>-><span Content); </span><span $resultStr</span> = ""<span ; </span><span $contentStr</span> = ""<span ; </span><span include</span>('baike.php'<span ); </span><span $contentStr</span> = getEncyclopediaInfo(<span $keyword</span><span ); </span><span $resultStr</span> = <span $this</span>->transmitText(<span $object</span>, <span $contentStr</span>, <span $funcFlag</span><span ); </span><span return</span> <span $resultStr</span><span ; } </span><span private</span> <span function</span> receiveEvent(<span $object</span><span ) { </span><span $contentStr</span> = ""<span ; </span><span switch</span> (<span $object</span>-><span Event) { </span><span case</span> "subscribe": <span $contentStr</span> = "欢迎关注"<span ; } </span><span $resultStr</span> = <span $this</span>->transmitText(<span $object</span>, <span $contentStr</span><span ); </span><span return</span> <span $resultStr</span><span ; } </span><span private</span> <span function</span> transmitText(<span $object</span>, <span $content</span>, <span $flag</span> = 0<span ) { </span><span $textTpl</span> = "<span <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml></span>"<span ; </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span>, <span $flag</span><span ); </span><span return</span> <span $resultStr</span><span ; } } </span><span function</span> logger(<span $log_content</span><span ) { } </span>?>
欢迎关注方倍工作室微信,体验我们开发的功能及了解行业的最新动态