Micro-trust public Platform Development example PHP Development code mount SAE Platform (vi) Constellation language PHP parse XML file

Source: Internet
Author: User
Tags foreach format array return string variable
Today is to query the daily horoscope function, this function is very simple, on the micro-platform input constellation name plus an identifier (such as "+" number), the background program to match this identifier call the corresponding function.   Specific process: 1. Use the string split function explode (w3school) to get the constellation name.   2. Match the constellation name to the corresponding constellation number.   3. Pass this number to the appropriate API.   4. Receive data from the XML format returned by the API.   5. Process data in XML format.   6. Generate results.   in which I made the process as a beginner, in the fifth step took some effort. The XML format returned by the API is   [HTML]   <root>       <item>           <ti Tle> Integrated op </title>           <rank>3</rank>         &NBSP ; <value></value>       </item>       <item>       &NBS P   <title> Love horoscope </title>           <rank>3</rank>           <value></value>       </item>       <item> &nbs P         <title> work status </title>           <rank>2</rank&Gt           <value></value>       </item>       &NB Sp ...       <item>2013-11-16</item>   </root>       First I use file_get_. The contents () function reads the data file returned by the API into a string. The XML string is then loaded into the object with the Simplexml_load_string () function. Then use this object to read the data in each XML tag, but here I have a problem with the returned XML format files are [html]  <item>             & nbsp;<title>......</title>              <rank>......<rank>   & nbsp          <value>......<value>   </item>     This format so I use "Object"-> Item->title method can only read the data of the first item, here I use the approach of the end use [PHP]   foreach ($xml->item as $th) {       & nbsp         $content _title[$i] = $th->title;               $content _rank[$i] = $th->rank;             &NBSp; $content _value[$i] = $th->value;               $i + +;         &NBSP}     Iterates over this object "$xml" and uses a self-adding variable I to store each set of data in an array. The foreach statement is used to iterate through the array.   Each time the loop is performed, the value of the current array element is assigned to the value variable (the array pointer moves one by one)-and so on. syntax [PHP]   foreach (array as value)   {      code to is executed  }     example below The sub demonstrates a loop that can output the value of a given array: [PHP]  

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.