CURL的get和post请求(自认为完美,mark下)

2020年1月7日23:11:00 发表评论 132 次浏览
/**
  * Curl send get request, support HTTPS protocol
  * @param string $url The request url
  * @param string $refer The request refer
  * @param int $timeout The timeout seconds
  * @return mixed
  */
 function getRequest($url, $refer = "", $timeout = 10)
 {
     $ssl = stripos($url,'https://') === 0 ? true : false;
     $curlObj = curl_init();
     $options = [
         CURLOPT_URL => $url,
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_FOLLOWLOCATION => 1,
         CURLOPT_AUTOREFERER => 1,
         CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
         CURLOPT_TIMEOUT => $timeout,
         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
         CURLOPT_HTTPHEADER => ['Expect:'],
         CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
     ];
     if ($refer) {
         $options[CURLOPT_REFERER] = $refer;
     }
     if ($ssl) {
         //support https
         $options[CURLOPT_SSL_VERIFYHOST] = false;
         $options[CURLOPT_SSL_VERIFYPEER] = false;
     }
     curl_setopt_array($curlObj, $options);
     $returnData = curl_exec($curlObj);
     if (curl_errno($curlObj)) {
         //error message
         $returnData = curl_error($curlObj);
     }
     curl_close($curlObj);
     return $returnData;
 }
 /**
  * Curl send post request, support HTTPS protocol
  * @param string $url The request url
  * @param array $data The post data
  * @param string $refer The request refer
  * @param int $timeout The timeout seconds
  * @param array $header The other request header
  * @return mixed
  */
 function postRequest($url, $data, $refer = "", $timeout = 10, $header = [])
 {
     $curlObj = curl_init();
     $ssl = stripos($url,'https://') === 0 ? true : false;
     $options = [
         CURLOPT_URL => $url,
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_POST => 1,
         CURLOPT_POSTFIELDS => $data,
         CURLOPT_FOLLOWLOCATION => 1,
         CURLOPT_AUTOREFERER => 1,
         CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
         CURLOPT_TIMEOUT => $timeout,
         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
         CURLOPT_HTTPHEADER => ['Expect:'],
         CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
         CURLOPT_REFERER => $refer
     ];
     if (!empty($header)) {
         $options[CURLOPT_HTTPHEADER] = $header;
     }
     if ($refer) {
         $options[CURLOPT_REFERER] = $refer;
     }
     if ($ssl) {
         //support https
         $options[CURLOPT_SSL_VERIFYHOST] = false;
         $options[CURLOPT_SSL_VERIFYPEER] = false;
     }
     curl_setopt_array($curlObj, $options);
     $returnData = curl_exec($curlObj);
     if (curl_errno($curlObj)) {
         //error message
         $returnData = curl_error($curlObj);
     }
     curl_close($curlObj);
     return $returnData;
 }

CURL的get和post请求(自认为完美,mark下)

阿修罗

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: