现在的位置: 首页 > 综合 > 正文

采用curl库在PHP程序之间传递数组 http://blog.s135.com/post/285/

2019年11月01日 ⁄ 综合 ⁄ 共 4755字 ⁄ 字号 评论关闭

采用curl库在PHP程序之间传递数组
  [文章作者:张宴 本文版本:v1.1 最后修改:2007.08.23 转载请注明出处:http://blog.s135.com]

  最近在工作中遇到一个问题:a.php程序需要将接收到的数据同时写到“线上运行的正式数据库”和“进行开发调试的测试数据库”。而测试数据库可能经常会面临对表结构、字段、配置信息做调整等问题,很不稳定,发生错误的概率很高,如果用a.php程序同时写“正式数据库”和“测试数据库”,势必影响到线上运行的正式服务。

  于是,我想到用PHP curl扩展库将生成的$data数组post传递一份给b.php程序,然后a.php程序继续往下执行写“正式数据库”的代码。a.php程序将$data数组传递给b.php程序就完事了,至于b.php如何处理,就不关a.php的事了,b.php程序即使写“测试数据库”失败,也不会对a.php程序造成影响。

  点击在新窗口中浏览此图片

  按照这种思路,我写了a.php和b.php的代码:

  a.php程序源代码:

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

  b.php程序源代码:

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

  备注:这两段代码需要php curl扩展库的支持,查看phpinfo(),如果cURL support  enabled则表示支持curl库。
  1、Windows下的PHP开启curl库支持:
  打开php.ini,将extension=php_curl.dll前的;号去掉。

  2、Linux下的PHP开启curl库支持:
  编译PHP时在./configure后加上 --with-curl

抱歉!评论已关闭.