关于PHP递归数组代码分析

开发 后端
PHP程序需要将接收到的数据同时写到“线上运行的正式数据库”和“进行开发调试的测试数据库”,文章详细的介绍了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. <?php 
  2. $data["username"]="张宴";  
  3. $data["password"]="不知道";  
  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);//这里是b.php的访问地址,请自行修改  
  10. //}  
  11. //curl类  
  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. //是否显示头部信息  
  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);//设置curl超时秒数,例如将信息POST出去3秒钟后自动结束运行。  
  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递归数组代码:

  1. <?php    
  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. ?> 
责任编辑:田树 来源: it168
相关推荐

2009-11-26 09:06:35

PHP递归数组

2009-11-30 09:35:15

PHP递归算法

2009-11-16 16:23:10

PHP数组遍历

2009-11-16 15:56:46

PHP数组查询

2009-11-16 16:17:45

PHP数组排序

2009-11-18 11:30:26

PHP数组排序

2009-11-17 15:57:26

PHP数组合并

2009-11-17 17:07:01

PHP关联数组

2009-11-17 16:53:24

PHP递归算法

2009-11-17 09:46:31

PHP二维数组赋值

2009-11-16 17:59:13

PHP数组转字符串

2009-11-17 15:33:26

PHP数组元素

2010-05-13 17:00:10

IIS 7.0

2009-11-16 16:43:24

PHP数组删除

2009-11-24 18:37:55

PHP数组转换

2009-11-17 16:09:04

PHP二维数组排序

2009-11-18 13:24:05

PHP单元素模式

2011-08-24 15:42:38

LUA源代码

2011-07-19 09:46:00

Oracle数据库递归查询

2009-11-25 10:31:35

PHP数组实现单链表
点赞
收藏

51CTO技术栈公众号