自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于PHP遞歸數(shù)組代碼分析

開(kāi)發(fā) 后端
PHP程序需要將接收到的數(shù)據(jù)同時(shí)寫(xiě)到“線上運(yùn)行的正式數(shù)據(jù)庫(kù)”和“進(jìn)行開(kāi)發(fā)調(diào)試的測(cè)試數(shù)據(jù)庫(kù)”,文章詳細(xì)的介紹了PHP遞歸數(shù)組源碼分析。

我們大家都知道PHP是一種HTML內(nèi)嵌式的語(yǔ)言,PHP與微軟的ASP頗有幾分相似,都是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語(yǔ)言,語(yǔ)言的風(fēng)格有類似于C語(yǔ)言,現(xiàn)在被很多的網(wǎng)站編程人員廣泛的運(yùn)用。文章這里詳細(xì)的介紹一下PHP遞歸數(shù)組。PHP程序需要將接收到的數(shù)據(jù)同時(shí)寫(xiě)到“線上運(yùn)行的正式數(shù)據(jù)庫(kù)”和“進(jìn)行開(kāi)發(fā)調(diào)試的測(cè)試數(shù)據(jù)庫(kù)”。

#T#而測(cè)試數(shù)據(jù)庫(kù)可能經(jīng)常會(huì)面臨對(duì)表結(jié)構(gòu)、字段、配置信息做調(diào)整等問(wèn)題,很不穩(wěn)定,發(fā)生錯(cuò)誤的概率很高,如果用a.php程序同時(shí)寫(xiě)“正式數(shù)據(jù)庫(kù)”和 “測(cè)試數(shù)據(jù)庫(kù)”,勢(shì)必影響到線上運(yùn)行的正式服務(wù)。于是,我想到用PHP curl擴(kuò)展庫(kù)將生成的$data數(shù)組post傳遞一份給php程序,然后php程序繼續(xù)往下執(zhí)行寫(xiě)“正式數(shù)據(jù)庫(kù)”的代碼。php程序?qū)?data數(shù)組傳遞給php程序就完事了,至于php如何處理,就不關(guān)php的事了,php程序即使寫(xiě)“測(cè)試數(shù)據(jù)庫(kù)”失敗,也不會(huì)對(duì) php程序造成影響。

PHP遞歸數(shù)組源代碼:

  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的訪問(wèn)地址,請(qǐng)自行修改  
  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);//設(shè)置curl超時(shí)秒數(shù),例如將信息POST出去3秒鐘后自動(dòng)結(jié)束運(yùn)行。  
  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遞歸數(shù)組代碼:

  1. <?php    
  2. ignore_user_abort();//連線中斷后(例如關(guān)閉瀏覽器)仍然繼續(xù)執(zhí)行以下的腳本直到處理完畢。  
  3. set_time_limit(0);  
  4. $get_data = file_get_contents("php://input");  
  5. $explodeexplodedata = explode("&", $get_data);  
  6. foreach ($explodedata as $key => $value)//還原數(shù)組  
  7. {  
  8. list($realkey, $realvalue) = explode("=", $value);  
  9. $data[urldecode($realkey)] = urldecode($realvalue);  
  10. }  
  11. //現(xiàn)在$data數(shù)組已經(jīng)和a.php中的一樣了,接下來(lái),就可以根據(jù)自己的需要對(duì)$data數(shù)組進(jìn)行操作了。  
  12. //......  
  13. ?> 
責(zé)任編輯:田樹(shù) 來(lái)源: it168
相關(guān)推薦

2009-11-26 09:06:35

PHP遞歸數(shù)組

2009-11-30 09:35:15

PHP遞歸算法

2009-11-16 16:23:10

PHP數(shù)組遍歷

2009-11-16 15:56:46

PHP數(shù)組查詢

2009-11-16 16:17:45

PHP數(shù)組排序

2009-11-18 11:30:26

PHP數(shù)組排序

2009-11-17 15:57:26

PHP數(shù)組合并

2009-11-17 17:07:01

PHP關(guān)聯(lián)數(shù)組

2009-11-17 16:53:24

PHP遞歸算法

2009-11-17 09:46:31

PHP二維數(shù)組賦值

2009-11-16 17:59:13

PHP數(shù)組轉(zhuǎn)字符串

2010-05-13 17:00:10

IIS 7.0

2009-11-17 15:33:26

PHP數(shù)組元素

2009-11-16 16:43:24

PHP數(shù)組刪除

2009-11-18 13:24:05

PHP單元素模式

2009-11-17 16:09:04

PHP二維數(shù)組排序

2009-11-24 18:37:55

PHP數(shù)組轉(zhuǎn)換

2011-08-24 15:42:38

LUA源代碼

2009-11-25 10:31:35

PHP數(shù)組實(shí)現(xiàn)單鏈表

2011-07-12 17:33:09

PHP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)