About PHP recursive array Code Analysis _php Tutorial

Source: Internet
Author: User
We all know that PHP is an HTML embedded language, PHP and Microsoft's ASP quite a bit similar, is a server-side implementation of embedded HTML document scripting language, language style has similar to C language, is now widely used by many web site programmers. This article describes in detail the PHP recursive array. The PHP program needs to write the received data to the "official database running on line" and "Test Database for development debugging".

and the test database may often face the table structure, fields, configuration information to make adjustments and so on, very unstable, the probability of error is very high, if using a.php program at the same time write "official database" and "Test Database", will inevitably affect the online operation of the official services. So, I thought of using 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 that writes the "official database." PHP program to pass the $data array to the PHP program is done, as for how PHP processing, it is not the case of PHP, PHP program even if the "test database" failure, will not affect the PHP program.

PHP recursive array source code:

 
 
  1. php
  2. $data ["username"]= "Zhang Feast";
  3. $data ["Password"]= "do not know";
  4. $data ["IP"]= "192.168.0.18";
  5. Register_shutdown_function ("Post_data", $data);
  6. function Post_data ($data)
  7. //{
  8. $ Curl = New Curl_class ();
  9. $ Post = @ $curl- > post ("http://127.0.0.1/b.php", $data);//This is b.php's access address, please modify it 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 = /c8>', $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 to display header information
  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 in seconds, for example, post the message 3 seconds after the automatic end of the run.
  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, to close the browser) continue to execute the following script until processing is complete.
  3. Set_time_limit (0);
  4. $ Get_data = file_get_contents ("Php://input");
  5. $ Explode Explodedata = Explode ("&", $get _data);
  6. foreach ($explodedata as $key => $value)//restore array
  7. {
  8. List ($realkey, $realvalue) = explode ("=", $value);
  9. $data [UrlDecode ($realkey)] = UrlDecode ($realvalue);
  10. }
  11. Now that the $data array is the same as in the a.php, you can then manipulate the $data array to suit your needs.
  12. //......
  13. ?>

http://www.bkjia.com/PHPjc/446465.html www.bkjia.com true http://www.bkjia.com/PHPjc/446465.html techarticle we all know that PHP is an HTML embedded language, PHP and Microsoft's ASP quite a bit similar, is a server-side implementation of embedded HTML document scripting language, language style ...

  • Related Article

    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.