最近在工作中遇到一個問題: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 $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程序源代碼:
<?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
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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