This article mainly introduces in detail the simple implementation of PHP WeChat development to query the relevant information of WeChat selected articles. Interested friends can refer to it. I hope to be helpful.
Query some selected articles in WeChat with relatively large number of clicks.
Don’t forget to apply for apikey (you can get it by logging in to your Baidu account). The function to be completed is:
1. The user replies to "article", and the official account must return the number of the article category (For example, 9. Technology).
2. If the user replies wz9, 1, Tencent, the article with the keyword "Tencent" in the science and technology articles can be returned, and the first page will be displayed (wz9, 2, Tencent can return to the second page, The number of articles returned on each page can be customized, here I return 7 articles).
Detailed steps:
1. Reply to "article" and return the ids of all article categories. The following code is part of the responseMsg method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if (! empty ( $postStr )){
$postObj = simplexml_load_string( $postStr , 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj ->FromUserName;
$toUsername = $postObj ->ToUserName;
$keyword = trim( $postObj ->Content);
$time = time();
$msgtype = 'text';
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
|
Copy after login
$which = mb_substr($keyword, 0, 2, 'UTF-8');
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | elseif ( $which == "文章" ){
$ch = curl_init();
$url = 'http:
$header = array ('apikey: 你自己的apikey');
curl_setopt( $ch , CURLOPT_HTTPHEADER , $header );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch , CURLOPT_URL , $url );
$res = curl_exec( $ch );
$res = json_decode( $res , true);
foreach ( $res ['showapi_res_body']['typeList'] as $v ){
$article [] = $v ['id'] . "、" . $v ['name'];
}
sort( $article , SORT_NUMERIC);
foreach ( $article as $v ){
$contentStr .= $v . "\n" ;
}
$resultStr = sprintf( $textTpl , $fromUsername , $toUsername , $time , $msgtype , $contentStr );
echo $resultStr ;
exit ();
}
|
Copy after login
2. At this time, the $resultStr of echo is the classification of all articles. Users can choose their favorite category to view articles based on the category ID. For example, by replying to wz19, 1, basketball, you can view articles about basketball classified as sports.
The specific calling interface and function implementation code are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | elseif ( $which == "wz" ){
list( $art_id , $page_id , $keyname ) = split(',', $keyword );
$art_id = str_replace ('wz', '', $art_id );
$ch = curl_init();
$url = 'http:
$header = array ('apikey: 你自己的apikey');
curl_setopt( $ch , CURLOPT_HTTPHEADER , $header );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch , CURLOPT_URL , $url );
$res = curl_exec( $ch );
$res = json_decode( $res , true);
foreach ( $res ['showapi_res_body']['pagebean']['contentlist'] as $k => $v ){
if ( $k <= 6){
$arts [] = $v ;
} else {
break ;
}
}
$items = "" ;
foreach ( $arts as $v ){
$items .= "<item>
<Title><![CDATA[ " . $v['title'] . " ]]></Title>
<Description><![CDATA[ " . $v['title'] . " ]]></Description>
<PicUrl><![CDATA[ " . $v[" contentImg "] . " ]]></PicUrl>
<Url><![CDATA[ " . $v['url'] . " ]]></Url>
</item>";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>7</ArticleCount>
<Articles> " . $items . "
</Articles>
</xml> ";
$resultStr = sprintf( $textTpl , $fromUsername , $toUsername , $time );
echo $resultStr ;
exit ();
}
|
Copy after login
Don’t forget to fill in $header = array('apikey: '); Your own apikey, otherwise the interface will refuse to return your request.
Related recommendations:
Summary of common error messages in WeChat public account development and configuration
WeChat public account implements user management function
TP access to WeChat public account payment details
The above is the detailed content of PHP WeChat development to obtain WeChat selected articles. For more information, please follow other related articles on the PHP Chinese website!