<?php
namespace DtApp\ThinkLibrary\service\curl;
use DtApp\ThinkLibrary\Service;
use think\exception\HttpException;
class HttpService extends Service
{
private $url, $data, $cert, $output;
private $timeout = 60;
private $method = 'GET';
private $headers = 'application/json;charset=utf-8';
public function url(string $str): self
{
$this->url = $str;
return $this;
}
public function data($str): self
{
if (is_array($str)) {
$this->data = json_encode($str, JSON_UNESCAPED_UNICODE);
} else {
$this->data = $str;
}
return $this;
}
public function headers(string $str): self
{
$this->headers = $str;
return $this;
}
public function timeout(int $int): self
{
$this->timeout = $int;
return $this;
}
public function cert(string $sslCertPath, string $sslKeyPath): self
{
$this->cert = [
'key' => $sslKeyPath,
'cert' => $sslCertPath,
];
return $this;
}
public function get(): self
{
$this->method = 'GET';
return $this;
}
public function post(): self
{
$this->method = 'POST';
return $this;
}
public function xml(): self
{
$this->method = 'XML';
return $this;
}
public function file(): self
{
$this->method = 'FILE';
return $this;
}
public function toArray(bool $is = true)
{
if (!extension_loaded("curl")) {
throw new HttpException(404, '请开启curl模块!');
}
if ($this->method === 'GET') {
$this->httpGet();
} else if ($this->method === 'POST') {
$this->httpPost();
} else if ($this->method === 'XML') {
$this->httpXml();
} else if ($this->method === 'FILE') {
$this->httpFile();
} else {
throw new HttpException(404, '请求方式异常');
}
if (empty($is)) {
return $this->output;
}
if (is_array($this->output)) {
return $this->output;
}
return json_decode($this->output, true);
}
private function httpGet()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if (!empty($this->data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$this->output = $output;
return $this;
}
private function httpPost()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: ' . $this->headers));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$content = curl_exec($ch);
curl_close($ch);
$this->output = $content;
return $this;
}
private function httpXml()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
set_time_limit(0);
if (!empty($this->headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: ' . $this->headers));
}
$data = curl_exec($ch);
curl_close($ch);
$this->output = $data;
return $this;
}
private function httpFile()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
if (empty($this->cert)) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $this->cert['cert']);
curl_setopt($ch, CURLOPT_SSLKEY, $this->cert['key']);
} else if (strpos($this->url, 'https') === 0) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); }
if (!empty($this->headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: ' . $this->headers));
}
curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $response_body = substr($output, $header_size);
curl_close($ch);
$this->output = $response_body;
return $this;
}
}