first commit
This commit is contained in:
167
common/CurlApp.php
Normal file
167
common/CurlApp.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace app\common;
|
||||
|
||||
class CurlApp
|
||||
{
|
||||
private $ch = null;
|
||||
|
||||
private array $options = [];
|
||||
|
||||
private string $headerContentType = '';
|
||||
|
||||
private string $error = '';
|
||||
|
||||
const METHOD_POST = 'POST';
|
||||
|
||||
const METHOD_GET = 'GET';
|
||||
|
||||
const CONTENT_TYPE_JSON = 'Content-Type: application/json';
|
||||
|
||||
const CONTENT_TYPE_FORM_URLENCODED = 'Content-Type: application/x-www-form-urlencoded';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->ch = curl_init();
|
||||
$this->options[CURLOPT_CONNECTTIMEOUT] = 30;
|
||||
$this->options[CURLOPT_TIMEOUT] = 30;
|
||||
}
|
||||
|
||||
public function setUrl(string $url)
|
||||
{
|
||||
$this->options[CURLOPT_URL] = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCookie(string $cookie)
|
||||
{
|
||||
$this->options[CURLOPT_COOKIE] = $cookie;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setReturnTransfer(bool $isReturn = true)
|
||||
{
|
||||
$this->options[CURLOPT_RETURNTRANSFER] = $isReturn;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPostData($data)
|
||||
{
|
||||
$this->options[CURLOPT_POSTFIELDS] = $data;
|
||||
if ($this->headerContentType === self::CONTENT_TYPE_FORM_URLENCODED) {
|
||||
if (is_array($data)) {
|
||||
$this->options[CURLOPT_POSTFIELDS] = http_build_query($data);
|
||||
}
|
||||
} elseif ($this->headerContentType === self::CONTENT_TYPE_JSON) {
|
||||
if (is_array($data)) {
|
||||
$this->options[CURLOPT_POSTFIELDS] = json_encode($data);
|
||||
} else {
|
||||
$this->options[CURLOPT_POSTFIELDS] = ($data);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setHttpHeader(array $header = [])
|
||||
{
|
||||
foreach ($header as $key => $value) {
|
||||
$this->options[CURLOPT_HTTPHEADER][$key] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setProxy($ip, $port)
|
||||
{
|
||||
// 基本代理
|
||||
$this->options[CURLOPT_PROXY] = "{$ip}:{$port}";
|
||||
// 指定代理类型(可选:HTTP、SOCKS4、SOCKS5)
|
||||
$this->options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
|
||||
}
|
||||
|
||||
public function setContentType($contentType)
|
||||
{
|
||||
$this->headerContentType = $contentType;
|
||||
$this->options[CURLOPT_HTTPHEADER][] = $contentType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMethod($method = 'GET')
|
||||
{
|
||||
$this->options[CURLOPT_POST] = !(strtolower($method) == 'get');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execMulti($urls = []): array
|
||||
{
|
||||
$multiHandle = curl_multi_init();
|
||||
$handles = [];
|
||||
$responses = [];
|
||||
|
||||
foreach ($urls as $key => $url) {
|
||||
$ch = curl_init();
|
||||
$this->options[CURLOPT_CONNECTTIMEOUT] = 60;
|
||||
$this->options[CURLOPT_TIMEOUT] = 60;
|
||||
// 继承当前 CurlApp 的 options
|
||||
$options = $this->options;
|
||||
|
||||
$options[CURLOPT_URL] = $url;
|
||||
$options[CURLOPT_RETURNTRANSFER] = true;
|
||||
|
||||
curl_setopt_array($ch, $options);
|
||||
|
||||
curl_multi_add_handle($multiHandle, $ch);
|
||||
$handles[$key] = $ch;
|
||||
}
|
||||
|
||||
// 执行
|
||||
$running = null;
|
||||
do {
|
||||
$status = curl_multi_exec($multiHandle, $running);
|
||||
if ($running) {
|
||||
curl_multi_select($multiHandle, 1);
|
||||
}
|
||||
} while ($running && $status === CURLM_OK);
|
||||
|
||||
// 收集结果
|
||||
foreach ($handles as $key => $ch) {
|
||||
$responses[$key] = curl_multi_getcontent($ch);
|
||||
curl_multi_remove_handle($multiHandle, $ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
curl_multi_close($multiHandle);
|
||||
|
||||
return $responses;
|
||||
|
||||
}
|
||||
|
||||
public function exec()
|
||||
{
|
||||
$defaultOptions = [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_SSL_VERIFYHOST => false,
|
||||
];
|
||||
|
||||
foreach ($defaultOptions as $key => $val) {
|
||||
if (!isset($this->options[$key])) {
|
||||
$this->options[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
curl_setopt_array($this->ch, $this->options);
|
||||
$resp = curl_exec($this->ch);
|
||||
|
||||
if (curl_errno($this->ch)) {
|
||||
$this->error = curl_error($this->ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_close($this->ch);
|
||||
return $resp;
|
||||
}
|
||||
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user