我們大家都知道PHP是一種HTML內嵌式的語言,PHP與微軟的ASP頗有幾分相似,都是一種在服務器端執行的嵌入HTML文檔的腳本語言,語言的風格有類似於C語言,現在被很多的網站編程人員廣泛的運用。文章這裡詳細的介紹一下PHP遞歸數組。PHP程序需要將接收到的數據同時寫到“線上運行的正式數據庫”和“進行開發調試的測試數據庫”。
PHP遞歸數組源代碼:
- <?php
- $data["username"]="張宴";
- $data["password"]="不知道";
- $data["ip"]="192.168.0.18";
- //reGISter_shutdown_function("post_data", $data);
- //function post_data($data)
- //{
- $curl = new Curl_Class();
- $post = @$curl->post("http://127.0.0.1/b.php", $data);//這裡是b.php的訪問地址,請自行修改
- //}
- //curl類
- class Curl_Class
- {
- function Curl_Class()
- {
- return true;
- }
- function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ch = Curl_Class::create();
- if (false === $ch)
- {
- return false;
- }
- if (is_string($url) && strlen($url))
- {
- $ret = curl_setopt($ch, CURLOPT_URL, $url);
- }
- else
- {
- return false;
- }
- //是否顯示頭部信息
- curl_setopt($ch, CURLOPT_HEADER, false);
- //
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($username != '')
- {
- curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
- }
- $method = strtolower($method);
- if ('post' == $method)
- {
- curl_setopt($ch, CURLOPT_POST, true);
- if (is_array($fields))
- {
- $sets = array();
- foreach ($fields AS $key => $val)
- {
- $sets[] = $key . '=' . urlencode($val);
- }
- $fields = implode('&',$sets);
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
- }
- else if ('put' == $method)
- {
- curl_setopt($ch, CURLOPT_PUT, true);
- }
- //curl_setopt($ch, CURLOPT_PROGRESS, true);
- //curl_setopt($ch, CURLOPT_VERBOSE, true);
- //curl_setopt($ch, CURLOPT_MUTE, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);//設置curl超時秒數,例如將信息POST出去3秒鐘後自動結束運行。
- if (strlen($userAgent))
- {
- curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
- }
- if (is_array($httpHeaders))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
- }
- $ret = curl_exec($ch);
- if (curl_errno($ch))
- {
- curl_close($ch);
- return array(curl_error($ch), curl_errno($ch));
- }
- else
- {
- curl_close($ch);
- if (!is_string($ret) || !strlen($ret))
- {
- return false;
- }
- return $ret;
- }
- }
- function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
- if (false === $ret)
- {
- return false;
- }
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
- function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
- if (false === $ret)
- {
- return false;
- }
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
- function create()
- {
- $ch = null;
- if (!function_exists('curl_init'))
- {
- return false;
- }
- $ch = curl_init();
- if (!is_resource($ch))
- {
- return false;
- }
- return $ch;
- }
- }
- ?>
PHP遞歸數組代碼:
- <?php
- ignore_user_abort();//連線中斷後(例如關閉浏覽器)仍然繼續執行以下的腳本直到處理完畢。
- set_time_limit(0);
- $get_data = file_get_contents("php://input");
- $explodeexplodedata = explode("&", $get_data);
- foreach ($explodedata as $key => $value)//還原數組
- {
- list($realkey, $realvalue) = explode("=", $value);
- $data[urldecode($realkey)] = urldecode($realvalue);
- }
- //現在$data數組已經和a.php中的一樣了,接下來,就可以根據自己的需要對$data數組進行操作了。
- //......
- ?>