PHP micro-Credit Development query micro-Letter Selected article _php example

Source: Internet
Author: User

Check out some of the selected text in the micro-letter, a larger number of clicks.
Do not forget to apply for Apikey (login Baidu account can be obtained), to complete the function is:

1, the user replies "article", the public number to return the article classification number (for example 9, science and Technology).

2, users reply wz9,1, Tencent can return to the science and technology articles, the key word "Tencent" article, and show the first page (wz9,2, Tencent can return to the second page, each page to return the number of articles can be customized, here I put back 7).

Detailed steps:

1, reply "article", return the ID of all article classification. The following code is part of the Responsemsg method, feel not understand or first contact with micro-letter development, you can refer to my article: http://www.jb51.net/article/87252.htm

  if (!empty ($POSTSTR)) {
    
   //parse post XML as an object $postobj
   $postObj = simplexml_load_string ($postStr, ' SimpleXMLElement ', libxml_nocdata);
  
   $fromUsername = $POSTOBJ->fromusername; The user who requested the message
   $toUsername = $POSTOBJ->tousername;//"I" public id
   $keyword = Trim ($postObj->content); The message content sent by the user
   $time = time ();//timestamp
   $msgtype = ' text ';//Message type: text
   $textTpl = "<xml>
      <tousername ><! [cdata[%s]]></tousername>
      <fromusername><![ cdata[%s]]></fromusername>
      <CreateTime>%s</CreateTime>
      <msgtype><![ Cdata[%s]]></msgtype>
      <content><![ cdata[%s]]></content>
      </xml> ";

$which = mb_substr ($keyword, 0, 2, ' UTF-8 ');

ElseIf ($which = = "Article") {
    $ch = Curl_init ();
    $url = ' Http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_type ';
    $header = Array (' Apikey: Your own Apikey ');

    Add Apikey to Header
    curl_setopt ($ch, Curlopt_httpheader, $header);
    curl_setopt ($ch, Curlopt_returntransfer, 1);
    Execute HTTP request
    curl_setopt ($ch, Curlopt_url, $url);
    $res = curl_exec ($ch);
    $res = Json_decode ($res, true); Get article category name and ID

    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 ();


   }

2, at this time Echo's $resultstr is the classification of all articles. users according to the classification of the ID, you can choose their favorite categories to view the article, such as reply to wz19,1, basketball can view classified as sports of the article on basketball.
The code for the specific calling interface and implementation function is as follows:

ElseIf ($which = = "wz") {list ($art _id, $page _id, $keyname) = Split (', ', $keyword);

    $art _id = str_replace (' wz ', ', $art _id);
    $ch = Curl_init (); $url = ' http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_list?typeId= '. $art _id. ' &page= '. $page _id. ' &key= '.

    UrlEncode ($keyname);

    $header = Array (' Apikey: Your own Apikey ');
    Add Apikey to Header curl_setopt ($ch, Curlopt_httpheader, $header);
    curl_setopt ($ch, Curlopt_returntransfer, 1);
    Execute HTTP request 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 ();
 }

Don't forget $header = Array (' Apikey: '); The time to fill out your own apikey, otherwise the interface will refuse to return your request.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.