原理很簡單,就是使用curl庫
<?php
/**
* HttpCurl Curl模擬Http工具類
* author salamander
* time 2016 12/08
*/
class HttpCurl {
private $ch = null; // curl handle
private $headers = array();// request header
private $proxy = null; // http proxy
private $timeout = 5; // connnect timeout
private $httpParams = null;
public function __construct()
{
$this->ch = curl_init();
}
/**
* 設定http header
* @param $header
* @return $this
*/
public function setHeader($header) {
if(is_array($header)){
curl_setopt($this->ch, CURLOPT_HTTPHEADER , $header);
}
return $this;
}
/**
* 設定http 超時
* @param int $time
* @return $this
*/
public function setTimeout($time) {
// 不能小於等於0
if($time <= 0) {
$time = 5;
}
//只需要設定一個秒的數量就可以
curl_setopt($this->ch, CURLOPT_TIMEOUT, $time);
return $this;
}
/**
* 設定http 代理
* @param string $proxy
* @return $this
*/
public function setProxy($proxy) {
if($proxy){
curl_setopt ($this->ch, CURLOPT_PROXY, $proxy);
}
return $this;
}
/**
* 設定http 代理埠
* @param int $port
* @return $this
*/
public function setProxyPort($port) {
if(is_int($port)) {
curl_setopt($this->ch, CURLOPT_PROXYPORT, $port);
}
return $this;
}
/**
* 設定來源頁面
* @param string $referer
* @return $this
*/
public function setReferer($referer = ""){
if (!empty($referer))
curl_setopt($this->ch, CURLOPT_REFERER , $referer);
return $this;
}
/**
* 設定使用者代理
* @param string $agent
* @return $this
*/
public function setUserAgent($agent = "") {
if ($agent) {
// 模擬使用者使用的瀏覽器
curl_setopt($this->ch, CURLOPT_USERAGENT, $agent);
}
return $this;
}
/**
* http響應中是否顯示header,1表示顯示
* @param $show
* @return $this
*/
public function showResponseHeader($show) {
curl_setopt($this->ch, CURLOPT_HEADER, $show);
return $this;
}
/**
* 設定http請求的引數,get或post
* @param array $params
* @return $this
*/
public function setParams($params) {
$this->httpParams = $params;
return $this;
}
/**
* 設定證照路徑
* @param $file
*/
public function setCainfo($file) {
curl_setopt($this->ch, CURLOPT_CAINFO, $file);
}
/**
* 模擬GET請求
* @param string $url
* @param string $dataType
* @return bool|mixed
*/
public function get($url, $dataType = `text`) {
if(stripos($url, `https://`) !== FALSE) {
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
}
// 設定get引數
if(!empty($this->httpParams) && is_array($this->httpParams)) {
if(strpos($url, `?`) !== false) {
$url .= http_build_query($this->httpParams);
} else {
$url .= `?` . http_build_query($this->httpParams);
}
}
// end 設定get引數
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1 );
$content = curl_exec($this->ch);
$status = curl_getinfo($this->ch);
curl_close($this->ch);
if (isset($status[`http_code`]) && $status[`http_code`] == 200) {
if ($dataType == `json`) {
$content = json_decode($content, true);
}
return $content;
} else {
return FALSE;
}
}
/**
* 模擬POST請求
*
* @param string $url
* @param array $fields
* @param string $dataType
* @return mixed
*
* HttpCurl::post(`http://api.example.com/?a=123`, array(`abc`=>`123`, `efg`=>`567`), `json`);
* HttpCurl::post(`http://api.example.com/`, `這是post原始內容`, `json`);
* 檔案post上傳
* HttpCurl::post(`http://api.example.com/`, array(`abc`=>`123`, `file1`=>`@/data/1.jpg`), `json`);
*/
public function post($url, $dataType=`text`) {
if(stripos($url, `https://`) !== FALSE) {
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
}
curl_setopt($this->ch, CURLOPT_URL, $url);
// 設定post body
if(!empty($this->httpParams)) {
if(is_array($this->httpParams)) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->httpParams));
} else if(is_string($this->httpParams)) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->httpParams);
}
}
// end 設定post body
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($this->ch, CURLOPT_POST, true);
$content = curl_exec($this->ch);
$status = curl_getinfo($this->ch);
curl_close($this->ch);
if (isset($status[`http_code`]) && $status[`http_code`] == 200) {
if ($dataType == `json`) {
$content = json_decode($content, true);
}
return $content;
} else {
return FALSE;
}
}
}
使用舉例
echo (new HttpCurl())->setParams([`name` => `dfh`, `age` => 12])->get(`http://www.test.com`);