Skip to content

Commit

Permalink
Merge pull request #35 from gaobinzhan/master
Browse files Browse the repository at this point in the history
 supply: swoole setting
  • Loading branch information
kiss291323003 authored Feb 22, 2021
2 parents 62969df + 51ead65 commit 6f1f247
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 274 deletions.
206 changes: 153 additions & 53 deletions src/Traits/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getFollowLocation(): int
}

/**
* 重定向次数
* @param int $followLocation
* @return $this
*/
Expand All @@ -56,6 +57,7 @@ public function setFollowLocation(int $followLocation)
}

/**
* 记录重定向多少次
* @return int
*/
public function getRedirected(): int
Expand Down Expand Up @@ -98,6 +100,7 @@ public function getCookies(): array
}

/**
* 设置cookie
* @param array $cookies
* @return $this
*/
Expand All @@ -107,7 +110,12 @@ public function setCookies(array $cookies)
return $this;
}


/**
* @param array $header
* @param bool $isMerge
* @param bool $strtolower
* @return $this
*/
public function setHeaders(array $header, $isMerge = true, $strtolower = true)
{
if (empty($header)) {
Expand All @@ -125,7 +133,11 @@ public function setHeaders(array $header, $isMerge = true, $strtolower = true)
return $this;
}


/**
* @param string $userName
* @param string $password
* @return $this
*/
public function setBasicAuth(string $userName, string $password)
{
$basicAuthToken = base64_encode("{$userName}:{$password}");
Expand Down Expand Up @@ -173,6 +185,56 @@ public function addCookies(array $cookies, $isMerge = true)
return $this;
}

/**
* 设置为Xml请求
* @return $this
*/
public function setContentTypeXml()
{
$this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_XML);
return $this;
}

/**
* 设置为FromData请求
* @return $this
*/
public function setContentTypeFormData()
{
$this->setContentType(HttpClient::CONTENT_TYPE_FORM_DATA);
return $this;
}

/**
* 设置为FromUrlencoded请求
* @return $this
*/
public function setContentTypeFormUrlencoded()
{
$this->setContentType(HttpClient::CONTENT_TYPE_X_WWW_FORM_URLENCODED);
return $this;
}

/**
* 设置为XMLHttpRequest请求
* @return $this
*/
public function setXMLHttpRequest()
{
$this->setHeader('x-requested-with', 'xmlhttprequest');
return $this;
}

/**
* 设置为Json请求
* @return $this
*/
public function setContentTypeJson()
{
$this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_JSON);
return $this;
}

/**
* 协程客户端设置项
* @var array
Expand All @@ -187,74 +249,148 @@ public function getClientSetting(): array
return $this->clientSetting;
}


/**
* 总超时,包括连接、发送、接收所有超时
* @param float $timeout
* @return $this
*/
public function setTimeout(float $timeout)
{
$this->clientSetting['timeout'] = $timeout;
return $this;
}

/**
* 连接超时,会覆盖第一个总的 timeout
* @param float $connectTimeout
* @return $this
*/
public function setConnectTimeout(float $connectTimeout)
{
$this->clientSetting['connect_timeout'] = $connectTimeout;
return $this;
}

/**
* 接收超时,会覆盖第一个总的 timeout
* @param float $readTimeout
* @return $this
*/
public function setReadTimeout(float $readTimeout)
{
$this->clientSetting['read_timeout'] = $readTimeout;
return $this;
}

/**
* 发送超时,会覆盖第一个总的 timeout
* @param float $writeTimeout
* @return $this
*/
public function setWriteTimeout(float $writeTimeout)
{
$this->clientSetting['write_timeout'] = $writeTimeout;
return $this;
}

/**
* 长连接
* @param bool $keepAlive
* @return $this
*/
public function setKeepAlive(bool $keepAlive = true)
{
$this->clientSetting['keep_alive'] = $keepAlive;
return $this;
}

/**
* 验证服务端证书
* @param bool $sslVerifyPeer
* @param false $sslAllowSelfSigned 允许自签名证书
* @return $this
*/
public function setSslVerifyPeer(bool $sslVerifyPeer = true, $sslAllowSelfSigned = false)
{
$this->clientSetting['ssl_verify_peer'] = $sslVerifyPeer;
$this->clientSetting['ssl_allow_self_signed'] = $sslAllowSelfSigned;
return $this;
}

/**
* 设置服务器主机名称
* @param string $sslHostName
* @return $this
*/
public function setSslHostName(string $sslHostName)
{
$this->clientSetting['ssl_host_name'] = $sslHostName;
return $this;
}

/**
* 设置 ssl_verify_peer 为 true 时,用来验证远端证书所用到的 CA 证书。本选项值为 CA 证书在本地文件系统的全路径及文件名
* @param string $sslCafile
* @return $this
*/
public function setSslCafile(string $sslCafile)
{
$this->clientSetting['ssl_cafile'] = $sslCafile;
return $this;
}

/**
* 如果未设置 ssl_cafile,或者 ssl_cafile 所指的文件不存在时,会在 ssl_capath 所指定的目录搜索适用的证书。该目录必须是已经经过哈希处理的证书目录。
* @param string $sslCapath
* @return $this
*/
public function setSslCapath(string $sslCapath)
{
$this->clientSetting['ssl_capath'] = $sslCapath;
return $this;
}

/**
* ssl cert
* @param string $sslCertFile
* @return $this
*/
public function setSslCertFile(string $sslCertFile)
{
$this->clientSetting['ssl_cert_file'] = $sslCertFile;
return $this;
}

/**
* ssl key
* @param string $sslKeyFile
* @return $this
*/
public function setSslKeyFile(string $sslKeyFile)
{
$this->clientSetting['ssl_key_file'] = $sslKeyFile;
return $this;
}

/**
* 本地证书 ssl_cert_file 文件的密码
* @param $sslPassphrase
* @return $this
*/
public function setSslPassphrase($sslPassphrase)
{
$this->clientSetting['ssl_passphrase'] = $sslPassphrase;
return $this;
}

/**
* http_proxy
* @param string $proxyHost
* @param int $proxyPort
* @param string|null $proxyUser
* @param string|null $proxyPass
* @return $this
*/
public function setProxyHttp(string $proxyHost, int $proxyPort, string $proxyUser = null, string $proxyPass = null)
{
$this->clientSetting['http_proxy_host'] = $proxyHost;
Expand All @@ -270,6 +406,14 @@ public function setProxyHttp(string $proxyHost, int $proxyPort, string $proxyUse
return $this;
}

/**
* socket5 代理
* @param string $proxyHost
* @param int $proxyPort
* @param string|null $proxyUser
* @param string|null $proxyPass
* @return $this
*/
public function setProxySocks5(string $proxyHost, int $proxyPort, string $proxyUser = null, string $proxyPass = null)
{
$this->clientSetting['socks5_host'] = $proxyHost;
Expand All @@ -285,6 +429,12 @@ public function setProxySocks5(string $proxyHost, int $proxyPort, string $proxyU
return $this;
}

/**
* 机器有多个网卡的情况下,设置 bind_address 参数可以强制客户端 Socket 绑定某个网络地址。设置 bind_port 可以使客户端 Socket 使用固定的端口连接到外网服务器。
* @param string $bindAddress
* @param int $bindPort
* @return $this
*/
public function setSocketBind(string $bindAddress, int $bindPort)
{
$this->clientSetting['bind_address'] = $bindAddress;
Expand All @@ -309,54 +459,4 @@ public function setClientSettings(array $settings, $isMerge = true)
}
return $this;
}

/**
* 设置为Xml请求
* @return $this
*/
public function setContentTypeXml()
{
$this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_XML);
return $this;
}

/**
* 设置为FromData请求
* @return $this
*/
public function setContentTypeFormData()
{
$this->setContentType(HttpClient::CONTENT_TYPE_FORM_DATA);
return $this;
}

/**
* 设置为FromUrlencoded请求
* @return $this
*/
public function setContentTypeFormUrlencoded()
{
$this->setContentType(HttpClient::CONTENT_TYPE_X_WWW_FORM_URLENCODED);
return $this;
}

/**
* 设置为XMLHttpRequest请求
* @return $this
*/
public function setXMLHttpRequest()
{
$this->setHeader('x-requested-with', 'xmlhttprequest');
return $this;
}

/**
* 设置为Json请求
* @return $this
*/
public function setContentTypeJson()
{
$this->setContentType(HttpClient::CONTENT_TYPE_APPLICATION_JSON);
return $this;
}
}
Loading

0 comments on commit 6f1f247

Please sign in to comment.