• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
百分百
http://www.qrw100.com
博客园    首页    新随笔    联系   管理    订阅  订阅

采用curl库在PHP程序之间传递数组

最近在工作中遇到一个问题: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程序源代码:

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


  b.php程序源代码:

view plaincopy to clipboardprint?
  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

posted @ 2011-11-18 09:50  爱尚美  阅读(527)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3