最近在工作中遇到一個(gè)問題:a.php程序需要將接收到的數(shù)據(jù)同時(shí)寫到“線上運(yùn)行的正式數(shù)據(jù)庫”和“進(jìn)行開發(fā)調(diào)試的測試數(shù)據(jù)庫”。而測試數(shù)據(jù)庫可能經(jīng) 常會(huì)面臨對表結(jié)構(gòu)、字段、配置信息做調(diào)整等問題,很不穩(wěn)定,發(fā)生錯(cuò)誤的概率很高,如果用a.php程序同時(shí)寫“正式數(shù)據(jù)庫”和“測試數(shù)據(jù)庫”,勢必影響到 線上運(yùn)行的正式服務(wù)。
于是,我想到用PHP curl擴(kuò)展庫將生成的$data數(shù)組post傳遞一份給b.php程序,然后a.php程序繼續(xù)往下執(zhí)行寫“正式數(shù)據(jù)庫”的代碼。a.php程序 將$data數(shù)組傳遞給b.php程序就完事了,至于b.php如何處理,就不關(guān)a.php的事了,b.php程序即使寫“測試數(shù)據(jù)庫”失敗,也不會(huì)對 a.php程序造成影響。
按照這種思路,我寫了a.php和b.php的代碼:
a.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);//設(shè)置curl超時(shí)秒數(shù),例如將信息POST出去3秒鐘后自動(dòng)結(jié)束運(yùn)行。 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程序源代碼:
<?php ignore_user_abort();//連線中斷后(例如關(guān)閉瀏覽器)仍然繼續(xù)執(zhí)行以下的腳本直到處理完畢。 set_time_limit(0); $get_data = file_get_contents("php://input"); $explodedata = explode("&", $get_data); foreach ($explodedata as $key => $value)//還原數(shù)組 { list($realkey, $realvalue) = explode("=", $value); $data[urldecode($realkey)] = urldecode($realvalue); } //現(xiàn)在$data數(shù)組已經(jīng)和a.php中的一樣了,接下來,就可以根據(jù)自己的需要對$data數(shù)組進(jìn)行操作了。 //...... ?>
?
備注:這兩段代碼需要php curl擴(kuò)展庫的支持,查看phpinfo(),如果cURL support ?enabled則表示支持curl庫。
1、Windows下的PHP開啟curl庫支持:
打開php.ini,將extension=php_curl.dll前的;號去掉。
2、Linux下的PHP開啟curl庫支持:
編譯PHP時(shí)在./configure后加上 --with-curl
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
