<?php
/**
* Create by PhpStorm
* Author: pd
* Time: 2020/2/27 10:32
* curl模拟请求
*/
class DHttpClient
{
private $timeout = 30;// 超时时间,默认为30s
private $ssl_verify_peer = false;// https请求是否验证证书
private $ssl_verity_host = false;// https请求是否验证host
private $proxy_url = '';// 代理服务器地址
private $proxy_port = '';// 代理服务器端口
private $http_version = CURL_HTTP_VERSION_1_1;// http版本
private $curl;
/**
* @return int
*/
public function getHttpVersion()
{
return $this->http_version;
}
/**
* 设置http版本
*
* @param int $http_version
* @return DHttpClient
*/
public function setHttpVersion($http_version)
{
$this->http_version = $http_version;
return $this;
}
/**
* 设置当进行https请求时是否验证证书
*
* @param bool $ssl_verify_peer
* @return DHttpClient
*/
public function setSslVerifyPeer($ssl_verify_peer)
{
$this->ssl_verify_peer = $ssl_verify_peer;
return $this;
}
/**
* 设置当进行https请求时是否验证host
*
* @param bool $ssl_verity_host
* @return DHttpClient
*/
public function setSslVerityHost($ssl_verity_host)
{
$this->ssl_verity_host = $ssl_verity_host;
return $this;
}
/**
* @return bool
*/
public function isSslVerifyPeer()
{
return $this->ssl_verify_peer;
}
/**
* @return bool
*/
public function isSslVerityHost()
{
return $this->ssl_verity_host;
}
/**
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* 设置请求超时时间
*
* @param int $timeout
* @return DHttpClient
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* @return string
*/
public function getProxyUrl()
{
return $this->proxy_url;
}
/**
* 设置代理服务器地址
*
* @param string $proxy_url
* @return DHttpClient
*/
public function setProxyUrl($proxy_url)
{
$this->proxy_url = $proxy_url;
return $this;
}
/**
* @return string
*/
public function getProxyPort()
{
return $this->proxy_port;
}
/**
* 设置代理服务器端口
*
* @param string $proxy_port
* @return DHttpClient
*/
public function setProxyPort($proxy_port)
{
$this->proxy_port = $proxy_port;
return $this;
}
/**
* 检查是否需要对参数执行json_encode
* @param $request DRequest
* @return bool
*/
private function isNeedJsonEncode(DRequest $request): bool
{
$need = true;
foreach ($request->getHeaders() as $header) {
if (strpos($header, 'application/json') ||
strpos($header, 'urlencoded')) $need = false;
}
return $need;
}
/**
* 根据参数配置curl
*
* @param $request DRequest
*/
private function initCurl($request)
{
$this->curl = curl_init();
// 设置url
curl_setopt($this->curl, CURLOPT_URL, $request->getUrl());
// 设置请求方式,默认不设置的话为get请求
switch ($request->getMethod()) {
case 'post':
curl_setopt($this->curl, CURLOPT_POST, 1);
//传递数据
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
$this->isNeedJsonEncode($request) ? json_encode($request->getParam(), JSON_UNESCAPED_UNICODE) : $request->getParam());
break;
}
// 设置超时时间
curl_setopt($this->curl, CONNECTION_TIMEOUT, $this->timeout);
// 设定返回的数据是否自动显示
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
// https请求是否验证证书和hosts
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, $this->ssl_verify_peer);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, $this->ssl_verity_host);
// 是否返回响应头
curl_setopt($this->curl, CURLOPT_HEADER, true);
if (!is_null($request->getSaveCookieJar())) {
// 把返回来的cookie信息保存在$cookie_jar文件中
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $request->getSaveCookieJar());
}
if (!is_null($request->getUseCookieJar())) {
// 使用保存的cookie文件
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $request->getUseCookieJar());
}
// 设定是否输出页面内容
curl_setopt($this->curl, CURLOPT_NOBODY, false);
// 设置请求头
if (!is_null($request->getHeaders()) && count($request->getHeaders()) > 0) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $request->getHeaders());
}
if (!is_null($this->proxy_port) && !is_null($this->proxy_url)) {
// 设置代理服务器
curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy_url);
curl_setopt($this->curl, CURLOPT_PROXYPORT, $this->proxy_port);
}
}
/**
* 执行请求
*
* @param $request DRequest
* @return DResponse
*/
public function execute($request)
{
$this->initCurl($request);
return $this->handleResponse();
}
/**
* 处理请求结果
*
* @return DResponse
*/
private function handleResponse()
{
$result = curl_exec($this->curl);
$response = new DResponse();
// 获取请求结果信息
$responseCode = curl_getinfo($this->curl, CURLINFO_RESPONSE_CODE);
$contentLength = curl_getinfo($this->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$response->statusCode = $responseCode;
$response->contentLength = $contentLength;
if ($result == false) {
// 请求出错
$response->responseStr = curl_error($this->curl);
} else {
$contentType = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE);
$response->contentType = $contentType;
// 解析http数据流
$list = explode("\r\n\r\n", $result);
$size = count($list);
$body = trim($list[$size - 1], "\xEF\xBB\xBF");// 去除bom头
$header = '';
$size = $size - 1;
for ($i = 0; $i < $size; $i++) {
$header .= $list[$i];
}
// 从响应头中解析出cookie
preg_match_all("/Set-Cookie: (.*?);/s", $header, $matches);
$cookieList = array();
foreach ($matches[1] as $value) {
$cookie = new DCookie(mb_substr($value, 0, mb_strpos($value, '=')), mb_substr($value, mb_strpos($value, '=') + 1));
$cookie->key = mb_substr($value, 0, mb_strpos($value, '='));
$cookieList[] = $cookie;
}
if (count($cookieList) > 0) {
$response->cookieList = $cookieList;
}
$response->header = $this->handleResponseHeader(mb_substr($result, 0, curl_getinfo($this->curl, CURLINFO_HEADER_SIZE)));
$response->responseStr = $body;
// var_dump($dCookieList);
// var_dump($body);
}
curl_close($this->curl);
return $response;
}
/**
* 从响应结果解析出响应头
*
* @param $headStr
* @return array
*/
php中的http请求工具
需积分: 0 17 浏览量
2022-03-24
16:56:27
上传
评论
收藏 7KB ZIP 举报
大东Pd
- 粉丝: 11
- 资源: 2
最新资源
- YOLOV4-TINY权重文件
- 以下是一个使用贪心算法解决多机调度问题的基本步骤0.txt
- 基于大数据的房产估价是近年来随着技术的发展而兴起的一种新型估价方法.txt
- 企业供应链管理系统v3.rar
- 富芮坤FR8016HA蓝牙开发板使用手册+硬件PCB图+封装库+DEMO演示软件源代码.zip
- 基于YOLOv7的芯片表面缺陷检测系统
- 京东物流 数字化供应链综合研究报告2018.rar
- 基于YOLOv7的植物虫害识别&防治系统
- 2000.1-2023.8中国经济政策不确定性指数月度数据.xlsx
- Screenshot_2024-04-21-20-42-15-443_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0