关于PHP递归数组代码分析

我们大家都知道PHP是一种HTML内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。文章这里详细的介绍一下PHP递归数组。PHP程序需要将接收到的数据同时写到“线上运行的正式数据库”和“进行开发调试的测试数据库”。

#T#而测试数据库可能经常会面临对表结构、字段、配置信息做调整等问题,很不稳定,发生错误的概率很高,如果用a.php程序同时写“正式数据库”和 “测试数据库”,势必影响到线上运行的正式服务。于是,我想到用PHP curl扩展库将生成的$data数组post传递一份给php程序,然后php程序继续往下执行写“正式数据库”的代码。php程序将$data数组传递给php程序就完事了,至于php如何处理,就不关php的事了,php程序即使写“测试数据库”失败,也不会对 php程序造成影响。

PHP递归数组源代码:

 
 
  1. $data["username"]="张宴";
  2. $data["password"]="不知道";
  3. $data["ip"]="192.168.0.18";
  4. //reGISter_shutdown_function("post_data", $data);
  5. //function post_data($data)
  6. //{
  7. $curl = new Curl_Class();
  8. $post = @$curl->post("http://127.0.0.1/b.php", $data);//这里是b.php的访问地址,请自行修改
  9. //}
  10. //curl类
  11. class Curl_Class
  12. {
  13. function Curl_Class()
  14. {
  15. return true;
  16. }
  17. function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '')
  18. {
  19. $ch = Curl_Class::create();
  20. if (false === $ch)
  21. {
  22. return false;
  23. }
  24. if (is_string($url) && strlen($url))
  25. {
  26. $ret = curl_setopt($ch, CURLOPT_URL, $url);
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. //是否显示头部信息
  33. curl_setopt($ch, CURLOPT_HEADER, false);
  34. //
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  36. if ($username != '')
  37. {
  38. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  39. }
  40. $method = strtolower($method);
  41. if ('post' == $method)
  42. {
  43. curl_setopt($ch, CURLOPT_POST, true);
  44. if (is_array($fields))
  45. {
  46. $sets = array();
  47. foreach ($fields AS $key => $val)
  48. {
  49. $sets[] = $key . '=' . urlencode($val);
  50. }
  51. $fields = implode('&',$sets);
  52. }
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  54. }
  55. else if ('put' == $method)
  56. {
  57. curl_setopt($ch, CURLOPT_PUT, true);
  58. }
  59. //curl_setopt($ch, CURLOPT_PROGRESS, true);
  60. //curl_setopt($ch, CURLOPT_VERBOSE, true);
  61. //curl_setopt($ch, CURLOPT_MUTE, false);
  62. curl_setopt($ch, CURLOPT_TIMEOUT, 3);//设置curl超时秒数,例如将信息POST出去3秒钟后自动结束运行。
  63. if (strlen($userAgent))
  64. {
  65. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  66. }
  67. if (is_array($httpHeaders))
  68. {
  69. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  70. }
  71. $ret = curl_exec($ch);
  72. if (curl_errno($ch))
  73. {
  74. curl_close($ch);
  75. return array(curl_error($ch), curl_errno($ch));
  76. }
  77. else
  78. {
  79. curl_close($ch);
  80. if (!is_string($ret) || !strlen($ret))
  81. {
  82. return false;
  83. }
  84. return $ret;
  85. }
  86. }
  87. function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
  88. {
  89. $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  90. if (false === $ret)
  91. {
  92. return false;
  93. }
  94. if (is_array($ret))
  95. {
  96. return false;
  97. }
  98. return $ret;
  99. }
  100. function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
  101. {
  102. $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
  103. if (false === $ret)
  104. {
  105. return false;
  106. }
  107. if (is_array($ret))
  108. {
  109. return false;
  110. }
  111. return $ret;
  112. }
  113. function create()
  114. {
  115. $ch = null;
  116. if (!function_exists('curl_init'))
  117. {
  118. return false;
  119. }
  120. $ch = curl_init();
  121. if (!is_resource($ch))
  122. {
  123. return false;
  124. }
  125. return $ch;
  126. }
  127. }
  128. ?>

PHP递归数组代码:

 
 
  1.   
  2. ignore_user_abort();//连线中断后(例如关闭浏览器)仍然继续执行以下的脚本直到处理完毕。
  3. set_time_limit(0);
  4. $get_data = file_get_contents("php://input");
  5. $explodeexplodedata = explode("&", $get_data);
  6. foreach ($explodedata as $key => $value)//还原数组
  7. {
  8. list($realkey, $realvalue) = explode("=", $value);
  9. $data[urldecode($realkey)] = urldecode($realvalue);
  10. }
  11. //现在$data数组已经和a.php中的一样了,接下来,就可以根据自己的需要对$data数组进行操作了。
  12. //......
  13. ?>

本文标题:关于PHP递归数组代码分析
文章路径:http://www.shufengxianlan.com/qtweb/news23/461923.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联