WeChat public platform development instance PHP development code mounting SAE platform (6) constellation story php parsing xml file

Source: Internet
Author: User
What we bring today is the function of querying Daily horoscope. This function is very simple. enter the constellation name on the platform and add an identifier (for example, "+ ), the background program matches this identifier to call the corresponding function. Specific process: 1. use the string splitting function explode (w3school) to obtain the daily Horoscope query function. This function is very simple, enter the constellation name and an identifier (for example, "+") on the platform. the background program matches this identifier to call the corresponding function. The specific process is as follows: 1. use the string splitting function explode (w3school) to obtain the constellation name. 2. match the constellation name with the corresponding Constellation number. 3. pass this number to the corresponding API. 4. receive xml format data returned by the API. 5. process data in xml format. 6. Generate the result. As a beginner, I made some effort in step 5. The xml format returned by the API is [html]. Overall operation 3 Love Fortune 3 Working condition 2 ...... First, I use the file_get_contents () function to read the data file returned by the API into a string. Then, use the simplexml_load_string () function to load the XML string into the object. Then use this object to read the data in each xml tag, but here I encountered a problem, the returned xml format files are all [html] ...... ...... ...... In this way, the "object"-> item-> title method can only read the data of the first item, here I use [php] foreach ($ xml-> item as $ th) {$ content_title [$ I] = $ th-> title; $ content_rank [$ I] = $ th-> rank; $ content_value [$ I] = $ th-> value; $ I ++ ;} traverse this object "$ xml" and store each group of data in an array using an auto-incrementing variable I. The foreach statement is used to traverse arrays cyclically. For each loop, the value of the current array element is assigned to the value variable (the array pointer is moved one by one), and so on. Syntax [php] foreach (array as value) {code to be executed;} the example below demonstrates a loop, which can output the value of the given array: [php] ";}?>The program source code is as follows: [php] function xingzuo ($ keyword) {if (make_xingzuo ($ keyword) {$ keyword = make_xingzuo ($ keyword); $ qurl =' http://api.uihoo.com/astro/astro.http.php?fun=day&id= '. $ Keyword. '& format = XML'; $ content = file_get_contents ($ qurl); $ xml = simplexml_load_string ($ content); $ I = 0; foreach ($ xml-> item as $ th) {$ content_title [$ I] = $ th-> title; $ content_rank [$ I] = $ th-> rank; $ content_value [$ I] = $ th-> value; $ I ++;} return "horoscope: \ n overall fortune :". $ content_rank [0]. "Stars \ n love fortune :". $ content_rank [1]. "Star \ n learning status :". $ content_rank [2]. "Stars \ n financial fortune :". $ content_rank [3]. "Star \ n Health Index :". $ content_value [4]. "\ n lucky color :". $ content_value [6]. "\ n lucky number :". $ content_value [7]. "\ n fast-matching constellation :". $ content_value [8]. "\ n summary :". $ content_value [9];} else return "the input constellation Xiaobian does not know ah >_<";} function make_xingzuo ($ keyword) {$ arr = array ('Aries' => '0', 'Taurus '=> '1', 'gemins' => '2 ', 'cancer '=> '3', 'Leo' => '4', 'virgo '=> '5', 'Libra' => '6 ', 'Libra '=> '6', 'Scorpio' => '7', 'Sagittarius '=> '8', 'capricorn' => '9 ', 'Aquarius '=> '10', 'pisces' => '11'); if (strtr ($ keyword, $ arr) <12 & strtr ($ keyword, $ arr)> 0) {return strtr ($ keyword, $ arr) ;}else {return false ;}}

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.