[文章作者:张宴 本文版本:v1.1 最后修改:2007.08.23 转载请注明出处:http://blog.zyan.cc]

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

  b.php程序源代码:


  备注:这两段代码需要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


Tags:
技术大类 » PHP/JS/Shell | 评论(29) | 引用(0) | 阅读(37934)
sx
2008-2-21 15:27
实际这个就是一个同步改异步的问题,可以有很多种解决方案,为什么要使用curl,curl的方式会增加一个http连接,而且curl本身的实现也有诸多问题。
少天 Email
2008-6-6 11:46
顶下~

偶来寻求帮助来了 呵呵
最近想用CURL实现获取邮件 联系人数据

弄了几天 出不来效果 教教我撒

MSN/GTALK :jstjob@gmail.com
jimmy
2008-10-31 13:39
回忆未来,这一句  $get_data = file_get_contents("php://input");  中的"php://input "是?
张宴 回复于 2008-10-31 13:50
PHP 输入/输出流,php://input 允许读取 POST 的原始数据。
jimmy
2008-10-31 15:09
恩,谢谢你的回复.我会一直向你学习的
jimmy
2008-10-31 19:21
unhappy那b.php,是怎么开始触发执行的呢
张宴 回复于 2008-11-2 14:49
a.php中$curl->post("http://127.0.0.1/b.php", $data);
b.php就执行了。
jimmy
2008-11-2 14:30
我的意思是,普通的form 会有个action,我们用curl来post后,它不action的,这怎么办?
张宴 回复于 2008-11-2 14:50
没听明白你的意思?
jimmy
2008-11-3 11:43
呵呵,是这样,在我的小例子里,我是想在修改的时候应用下.在列表页中从数据库中提取出数据后,我点修改的时候,不想在去提取数据库,而是想直接从列表页传递过来,但我用get方式又行不通,因为数据量肯定是get所承受不起的,所以我想模拟一种post的方式在不用表单的情况下.说到普通form,提交后,会根据一个action的地址,到达一个处理页面.用curl来post的时候,就没有这种效果了.对吧,,所以我才问的这个,不知道我说明白了没,指点我下,继续关注
hhlin
2008-11-21 23:03
如果 a.php 放在 www.abc.com 根目录, b.php 放在 www.asd.com 根目录,
那么访问 http://www.abc.com/a.php 触发不了 http://www.asd.com/b.php

问题在哪里.
DIY Email Homepage
2009-7-1 11:44
将此数组先序列化(serialize ,json)再传递不就好了????
aiway
2009-10-22 14:49
写的很好 支持 期待更多原创内容
请问下,利用fsockopen() 和使用curl那个会更快些?
a.php在发送数据的时候是否需要等待数据发送完呢?
一叶轩 Email
2009-11-28 15:32
我用这个类去采我自己的一个页面(此页面已关闭allow_url_fopen,返回的只是一串简单的json代码);

经过测试,这个类中的get就不好使了。

但是用这个取,就没有问题:

function curl_fetch($url) {
      $c = curl_init($url);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($c, CURLOPT_TIMEOUT,10);
      curl_setopt($c, CURLOPT_CONNECTTIMEOUT,1);
      curl_setopt($c, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
      $content = curl_exec($c);
      $err = curl_errno($c);
      $info = curl_getinfo($c);
      $succ = false;
      if (!$err && isset($info['http_code']) && 200 == $info['http_code']) {
          $succ = true;
      }
      //return array($succ, $content);
      return $content;
  }
一叶轩
2009-11-28 16:20
不好意思,是我搞错了,是我在get时没有指定好USERAGENT,这个类很好用,谢谢分享!!!
Hui
2010-8-16 12:40
如果多维数组只能用json_encode了
123
2011-10-28 17:38
如果我要模拟提交一个复选框的内容怎么做
coach factory outlet Email Homepage
2012-5-17 11:00
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:01
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:01
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:01
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
xujie777 Email
2012-5-18 16:21
We aim to make all our customers satisfy with our products. You will find a variety of Men's fashion louis vuitton uk, fashion Women's cheap Louis Vuitton bags in our store at affordable price.Thinking of interesting ways to cost a milestone birthday? louis vuitton online shop had one of the most distinctive distinctive celebrations.bakery along with living room operated by means of about three moment louis vuitton online Most effective Pastry Chef’s of the year Rammy Nominee Chef’s.
xujie777 Email
2012-5-18 16:21
The choices are likely to be basically countless seeing that louis vuitton outlet occurs with the help of completely new and also incredible concepts once in a while.Louis vuitton Wholesale Monogram Canvas HandbagsLouis Vuitton Collection Beach Handbags louis vuitton bags outlet Damier Canvas HandbagsLouis vuitton Mahina HandbagsLouis Vuitton Monogram Mini Lin HandbagsLouis Vuitton Monogram Multicolore HandbagsLouis vuitton Monogram Vernis HandbagsLouis Vuitton Wholesale Epi Leather HandbagsLouis Vuitton For Men HandbagsLouis Vuitton Damier Canvas WalletsLouis Vuitton Epi Leather WalletsLouis Vuitton Monogram Canvas WalletsLouis Vuitton Monogram Vernis WalleLouis Vuitton ShoesLouis Vuitton Men wallets.As the Authentic Louis Vuitton are so high-priced, so came the louis vuitton handbags outlet.
分页: 1/2 第一页 1 2 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]