1. 程式人生 > >Thinkphp POST 和 GET 傳值

Thinkphp POST 和 GET 傳值

field erro function array err ray ssl ati ext

不知道哪裏抄來的函數 ,哈哈

public function http($url, $params, $method = ‘GET‘, $header = array(), $multi = false){
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
/* 根據請求類型設置特定參數 */
switch(strtoupper($method)){
case ‘GET‘:
$opts[CURLOPT_URL] = $url . ‘?‘ . http_build_query($params);
break;
case ‘POST‘:
//判斷是否傳輸文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception(‘不支持的請求方式!‘);
}
/* 初始化並執行curl請求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception(‘請求發生錯誤:‘ . $error);
return $data;
}

//post調用

$this -> http($url, $requestData, ‘POST‘, array("Content-Type: application/x-www-form-urlencoded;charset=utf-8"));

//get 調用

$this -> http($url, $val, ‘GET‘, array("Content-type: text/html; charset=utf-8"));

Thinkphp POST 和 GET 傳值