diff --git a/wx/WxClient.php b/wx/WxClient.php index 82d9c28..427c124 100644 --- a/wx/WxClient.php +++ b/wx/WxClient.php @@ -15,6 +15,7 @@ class WxClient private $url = ''; private $isSSL = false; + private $agent = ''; const POST = 'post'; const GET = 'get'; @@ -71,6 +72,11 @@ class WxClient $this->url = $url; } + public function setAgent(string $agent) + { + $this->agent = $agent; + } + /** * @param bool $isSSL */ @@ -165,10 +171,12 @@ class WxClient { $host = $this->getHostIp(); $client = new Client($host, $port, $this->isSSL); + if (!empty($this->agent)) { + $this->header['User-Agent'] = $this->agent; + } if (!empty($this->header)) { $client->setHeaders($this->header); } - switch (strtolower($this->method)) { case self::GET: $client->get($url); @@ -219,25 +227,24 @@ class WxClient } $url = $url . '?' . $_data; } - curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置 curl_setopt($ch, CURLOPT_HEADER, FALSE); if (!empty($this->header)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header); } - + if (!empty($this->agent)) { + curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); + } if ($this->isSSL) { curl_setopt($ch, CURLOPT_SSLCERT, $this->header['ssl_cert_file']); curl_setopt($ch, CURLOPT_SSLKEY, $this->header['ssl_key_file']); } - curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);// 超时设置 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); - return $ch; }