做做笔记,有错误,望大神指点,多多讨论,多多留言!
/**
* GET请求
* @param $url
* @param array $data
* @param bool $https
* @param int $timeout
* @return bool|string
*/
function httpGet($url,$data=array(),$https=true,$timeout=60) {
if($url == "" || $timeout <= 0){
return false;
}
if(is_array($data)) {
$url = $url . '?' . http_build_query($data);
}
$curl = curl_init();
if ($https) {
//设置为https请求,不验证证书和hosts
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response_data = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
if(intval($status["http_code"])==200){
return $response_data;
}else{
return false;
}
}
/**
* 获取GET请求结果
* @param $url
* @return mixed
*/
function getData($url){
$method ="GET";
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
$ret = curl_exec($curl);
$all = json_decode($ret,true);
return $all;
}
/**
* POST请求
* @param $url
* @param array $data
* @param bool $https
* @param int $timeout
* @return bool|string
*/
function httpPost($url,$data=array(),$https=true,$timeout=60) {
if($url &#