PHP recursive array code analysis

Source: Internet
Author: User

We all know that PHP is an embedded HTML language. PHP is similar to Microsoft's ASP, and it is a script language that is executed on the server to embed HTML documents, the language style is similar to the C language and is widely used by many website programmers. This article introduces PHP recursive arrays in detail. The PHP program needs to write the received data to both the "official database running online" and the "test database for development and debugging ".

However, the test database may often face problems such as adjusting the table structure, fields, and configuration information. It is very unstable and has a high probability of errors. If a is used. php programs write "official database" and "Test Database" at the same time, which will inevitably affect the official services running online. So I want to use the PHP curl extension library to pass the generated $ data array post to the php program, and then the php program continues to execute the code to write the "official database. The php program will pass the $ data array to the php program. As for how php is handled, it will not be related to php. Even if the php program fails to write "Test Database, it will not affect the php program.

PHP recursive array source code:

 
 
  1. <?Php 
  2. $ Data ["username"] = "banquet ";
  3. $ Data ["password"] = "unknown ";
  4. $ Data ["ip"] = "192.168.0.18 ";
  5. // ReGISter_shutdown_function ("post_data", $ data );
  6. // Function post_data ($ data)
  7. //{
  8. $Curl=NewCurl_Class ();
  9. $Post= @ $ Curl->Post ("http: // 127.0.0.1/B. php", $ data); // The access address of B. php is here. Modify it by yourself.
  10. //}
  11. // Curl class
  12. Class Curl_Class
  13. {
  14. Function Curl_Class ()
  15. {
  16. Return true;
  17. }
  18. Function execute ($ method, $ url, $Fields='', $UserAgent='', $HttpHeaders='', $Username='', $Password='')
  19. {
  20. $Ch=Curl_Class: Create ();
  21. If (False===$ Ch)
  22. {
  23. Return false;
  24. }
  25. If (is_string ($ url) & strlen ($ url ))
  26. {
  27. $Ret=Curl_setopt($ Ch, CURLOPT_URL, $ url );
  28. }
  29. Else
  30. {
  31. Return false;
  32. }
  33. // Whether the header information is displayed
  34. Curl_setopt ($ ch, CURLOPT_HEADER, false );
  35. //
  36. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  37. If ($ username! = '')
  38. {
  39. Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password );
  40. }
  41. $Method=Strtolower($ Method );
  42. If ('post' = $ method)
  43. {
  44. Curl_setopt ($ ch, CURLOPT_POST, true );
  45. If (is_array ($ fields ))
  46. {
  47. $Sets=Array();
  48. Foreach ($ fields AS $Key=>$ Val)
  49. {
  50. $ Sets [] = $ key. '='. urlencode ($ val );
  51. }
  52. $Fields=Implode('&', $ Sets );
  53. }
  54. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
  55. }
  56. Else if ('put' = $ method)
  57. {
  58. Curl_setopt ($ ch, CURLOPT_PUT, true );
  59. }
  60. // Curl_setopt ($ ch, CURLOPT_PROGRESS, true );
  61. // Curl_setopt ($ ch, CURLOPT_VERBOSE, true );
  62. // Curl_setopt ($ ch, CURLOPT_MUTE, false );
  63. Curl_setopt ($ ch, CURLOPT_TIMEOUT, 3); // sets the curl timeout time in seconds. For example, the system automatically stops running after three seconds of posting information.
  64. If (strlen ($ userAgent ))
  65. {
  66. Curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent );
  67. }
  68. If (is_array ($ httpHeaders ))
  69. {
  70. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders );
  71. }
  72. $Ret=Curl_exec($ Ch );
  73. If (curl_errno ($ ch ))
  74. {
  75. Curl_close ($ ch );
  76. Return array (curl_error ($ ch), curl_errno ($ ch ));
  77. }
  78. Else
  79. {
  80. Curl_close ($ ch );
  81. If (! Is_string ($ ret) |! Strlen ($ ret ))
  82. {
  83. Return false;
  84. }
  85. Return $ ret;
  86. }
  87. }
  88. Function post ($ url, $ fields, $UserAgent='', $HttpHeaders='', $Username='', $Password='')
  89. {
  90. $Ret=Curl_Class: Execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password );
  91. If (False===$ Ret)
  92. {
  93. Return false;
  94. }
  95. If (is_array ($ ret ))
  96. {
  97. Return false;
  98. }
  99. Return $ ret;
  100. }
  101. Function get ($ url, $UserAgent='', $HttpHeaders='', $Username='', $Password='')
  102. {
  103. $Ret=Curl_Class: Execute ('get', $ url, '', $ userAgent, $ httpHeaders, $ username, $ password );
  104. If (False===$ Ret)
  105. {
  106. Return false;
  107. }
  108. If (is_array ($ ret ))
  109. {
  110. Return false;
  111. }
  112. Return $ ret;
  113. }
  114. Function create ()
  115. {
  116. $Ch=Null;
  117. If (! Function_exists ('curl _ init '))
  118. {
  119. Return false;
  120. }
  121. $Ch=Curl_init();
  122. If (! Is_resource ($ ch ))
  123. {
  124. Return false;
  125. }
  126. Return $ ch;
  127. }
  128. }
  129. ?> 

PHP recursive array code:

 
 
  1. <?Php
  2. Ignore_user_abort (); // after the connection is interrupted (for example, the browser is closed), the following script continues to be executed until the processing is completed.
  3. Set_time_limit (0 );
  4. $Get_data=File_get_contents("Php: // input ");
  5. $ExplodeExplodedata= Explode ("&", $ get_data );
  6. Foreach ($ explodedata as $Key=>$ Value) // restore the Array
  7. {
  8. List ($ realkey, $ realvalue) = explode ("=", $ value );
  9. $ Data [urldecode ($ realkey)] = urldecode ($ realvalue );
  10. }
  11. // Now the $ data array is the same as that in a. php. Next, you can operate the $ data array as needed.
  12. //......
  13. ?> 

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.