您的位置:首页 > 编程语言 > PHP开发

采用curl库在PHP程序之间传递数组[转载]

2010-11-26 17:56 295 查看
出处: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程序源代码:

view plaincopy to clipboardprint?

<?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
$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;
}

}
?>

  b.php程序源代码:

view plaincopy to clipboardprint?

<?php

ignore_user_abort();//连线中断后(例如关闭浏览器)仍然继续执行以下的脚本直到处理完毕。

set_time_limit(0);

$get_data = file_get_contents("php://input");

$explodedata = explode("&", $get_data);

foreach ($explodedata as $key => $value)//还原数组

{

list($realkey, $realvalue) = explode("=", $value);

$data[urldecode($realkey)] = urldecode($realvalue);

}

//现在$data数组已经和a.php中的一样了,接下来,就可以根据自己的需要对$data数组进行操作了。

//......

?>

<?php
ignore_user_abort();//连线中断后(例如关闭浏览器)仍然继续执行以下的脚本直到处理完毕。
set_time_limit(0);
$get_data = file_get_contents("php://input");
$explodedata = explode("&", $get_data);

foreach ($explodedata as $key => $value)//还原数组
{
list($realkey, $realvalue) = explode("=", $value);
$data[urldecode($realkey)] = urldecode($realvalue);
}
//现在$data数组已经和a.php中的一样了,接下来,就可以根据自己的需要对$data数组进行操作了。
//......
?>


  备注:这两段代码需要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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: