>>fun:httpPost/httpGET>>功能:向$url POST/GET 數據
來源:程序員人生 發布時間:2015-08-06 09:53:40 閱讀次數:3696次
>>>函數名:httpPost
功能:向$url POST xml數據
code:
/**
* [httpPost 向$url POST $data數據]
* @param [sting] $url [action url]
* @param [array] $data [數據數組 eg:$data = <xml><user>root</user><pwd>ooxx</pwd></xml>]
* @return [sting] $res [響應數據]
*/
function httpPost($url,$data){
//定義content-type為xml,注意是數組
$header[] = "Content-type:text/xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
//避免出現驗證毛病
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if(curl_errno($ch)){
print curl_error($ch);
}
curl_close($ch);
return $res;
}
>>>函數名:httpGet
功能:向$url POST $data數據
code:
/**
* [httpGet get $url 返回的數據]
* @param [sting] $url [目標url]
* @return [sting] $res [響應數據]
*/
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈