关于PHP递归数组代码分析
2013-09-04 21:47
645 查看
我们大家都知道PHP是一种HTML内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。文章这里详细的介绍一下PHP递归数组。PHP程序需要将接收到的数据同时写到“线上运行的正式数据库”和“进行开发调试的测试数据库”。
而测试数据库可能经常会面临对表结构、字段、配置信息做调整等问题,很不稳定,发生错误的概率很高,如果用a.php程序同时写“正式数据库”和 “测试数据库”,势必影响到线上运行的正式服务。于是,我想到用PHP curl扩展库将生成的$data数组post传递一份给php程序,然后php程序继续往下执行写“正式数据库”的代码。php程序将$data数组传递给php程序就完事了,至于php如何处理,就不关php的事了,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数组进行操作了。 //...... ?>
而测试数据库可能经常会面临对表结构、字段、配置信息做调整等问题,很不稳定,发生错误的概率很高,如果用a.php程序同时写“正式数据库”和 “测试数据库”,势必影响到线上运行的正式服务。于是,我想到用PHP curl扩展库将生成的$data数组post传递一份给php程序,然后php程序继续往下执行写“正式数据库”的代码。php程序将$data数组传递给php程序就完事了,至于php如何处理,就不关php的事了,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数组进行操作了。 //...... ?>
相关文章推荐
- 递归实现php数组转xml的代码分享
- PHP数组交集的优化代码分析
- 关于PHP中数组递归遍历的一些见解
- 递归实现php数组转xml的代码分享
- UCHome中关于公共函数(function_common.php)页面的代码分析(一)
- 递归遍历多维数组(树数据结构)的超级简单方式,并且可以递归超过200层,摘自<<PHP精粹:编写高效PHP代码>>
- UCHome中关于公共函数(function_common.php)页面的代码分析(二)
- PHP中关于指针数组的函数
- php数组添加与删除单元的常用函数实例分析
- 从一段经典错误代码说起——关于局部变量指针和函数传参的问题分析
- 关于递归运算的顺序测试代码
- php下将多个数组合并成一个数组的方法与实例代码
- php 数组的指针操作实现代码
- java中int与byte数组互转代码详细分析
- PHP数组实际占用内存大小的分析
- php在多维数组中根据键名快速查询其父键以及父键值的代码
- php中用foreach来操作数组的代码
- [zz终于搞懂]后缀数组-建数组代码分析