WeChat public platform development instance PHP development code mounting SAE platform (6) constellation thing language p

Source: Internet
Author: User
What we bring today is the function of querying daily horoscope. This function is very simple. Enter a 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

What we bring today is the function of querying daily horoscope. This function is very simple. Enter a 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

What we bring today is query dailyConstellationThis function is very simple.PlatformInput onConstellationName and 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 obtainConstellationName.

2. SetConstellationThe name matches with the correspondingConstellationId.

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

     
          
   Overall Operation        
   
    
3
           
       
      
          
   Love fortune        
   
    
3
           
       
      
          
   Working Condition        
   
    
2
           
       
  ......
  

  
 


First, I use the file_get_contents () function to return the data returned by the API. FileRead a string. Then use
            
  ……           
  
   ……
              
    
     ……
     
    
   
  
 

In this way, the "object"-> item-> title method can only read the data of the first item.
 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

foreach (array as value){    code to be executed;}

Example

The following example demonstrates a loop that outputs the value of a given array:

 ";}?>


Program SourceCodeAs follows:

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"ConstellationFortune: \ 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 quick ConfigurationConstellation: ". $ Content_value [8]." \ n Overview: ". $ content_value [9];} else return"ConstellationXiaobian does not know Ah> _ <";} function make_xingzuo ($ keyword) {$ arr = array ('aries '=> '0', 'taurus' => '1 ', 'gemins' => '2', 'loan' => '3', 'Leo '=> '4', 'virgo' => '5 ', 'library' => '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.