Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from 宇润/YurunHttp
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
dev
Branches (5)
Tags (53)
dev
master
v3
v2
v1
v4.0.0
v3.5.0
v3.4.6
v3.4.5
v3.4.4
v3.4.3
v3.4.2
v3.4.1
v3.4.0
v3.3.0
v3.2.6
v3.2.5
v3.2.4
v3.2.3
v3.2.2
v3.2.1
v3.2.0
v3.1.5
v3.1.4
v3.1.3
dev
Branches (5)
Tags (53)
dev
master
v3
v2
v1
v4.0.0
v3.5.0
v3.4.6
v3.4.5
v3.4.4
v3.4.3
v3.4.2
v3.4.1
v3.4.0
v3.3.0
v3.2.6
v3.2.5
v3.2.4
v3.2.3
v3.2.2
v3.2.1
v3.2.0
v3.1.5
v3.1.4
v3.1.3
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
dev
Branches (5)
Tags (53)
dev
master
v3
v2
v1
v4.0.0
v3.5.0
v3.4.6
v3.4.5
v3.4.4
v3.4.3
v3.4.2
v3.4.1
v3.4.0
v3.3.0
v3.2.6
v3.2.5
v3.2.4
v3.2.3
v3.2.2
v3.2.1
v3.2.0
v3.1.5
v3.1.4
v3.1.3
YurunHttp
/
src
/
HttpRequest.php
YurunHttp
/
src
/
HttpRequest.php
HttpRequest.php 28.37 KB
Copy Edit Raw Blame History
宇润 authored 2021年03月10日 08:52 +08:00 . 引入 phpstan 静态分析,并修复一些问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
<?php
namespace Yurun\Util;
use Yurun\Util\YurunHttp\Attributes;
use Yurun\Util\YurunHttp\Http\Psr7\Consts\MediaType;
use Yurun\Util\YurunHttp\Http\Psr7\UploadedFile;
use Yurun\Util\YurunHttp\Http\Request;
class HttpRequest
{
/**
* 处理器.
*
* @var \Yurun\Util\YurunHttp\Handler\IHandler|null
*/
private $handler;
/**
* 需要请求的Url地址
*
* @var string
*/
public $url;
/**
* 发送内容,可以是字符串、数组(支持键值、Yurun\Util\YurunHttp\Http\Psr7\UploadedFile,其中键值会作为html编码,文件则是上传).
*
* @var mixed
*/
public $content;
/**
* `curl_setopt_array()`所需要的第二个参数.
*
* @var array
*/
public $options = [];
/**
* 请求头.
*
* @var array
*/
public $headers = [];
/**
* Cookies.
*
* @var array
*/
public $cookies = [];
/**
* 失败重试次数,默认为0.
*
* @var int
*/
public $retry = 0;
/**
* 是否使用代理,默认false.
*
* @var bool
*/
public $useProxy = false;
/**
* 代理设置.
*
* @var array
*/
public $proxy = [];
/**
* 是否验证证书.
*
* @var bool
*/
public $isVerifyCA = false;
/**
* CA根证书路径.
*
* @var string|null
*/
public $caCert;
/**
* 连接超时时间,单位:毫秒.
*
* @var int
*/
public $connectTimeout = 30000;
/**
* 总超时时间,单位:毫秒.
*
* @var int
*/
public $timeout = 30000;
/**
* 下载限速,为0则不限制,单位:字节
*
* @var int|null
*/
public $downloadSpeed;
/**
* 上传限速,为0则不限制,单位:字节
*
* @var int|null
*/
public $uploadSpeed;
/**
* 用于连接中需要的用户名.
*
* @var string|null
*/
public $username;
/**
* 用于连接中需要的密码
*
* @var string|null
*/
public $password;
/**
* 请求结果保存至文件的配置.
*
* @var mixed
*/
public $saveFileOption = [];
/**
* 是否启用重定向.
*
* @var bool
*/
public $followLocation = true;
/**
* 最大重定向次数.
*
* @var int
*/
public $maxRedirects = 10;
/**
* 证书类型
* 支持的格式有"PEM" (默认值), "DER"和"ENG".
*
* @var string
*/
public $certType = 'pem';
/**
* 一个包含 PEM 格式证书的文件名.
*
* @var string
*/
public $certPath = '';
/**
* 使用证书需要的密码
*
* @var string
*/
public $certPassword = null;
/**
* certType规定的私钥的加密类型,支持的密钥类型为"PEM"(默认值)、"DER"和"ENG".
*
* @var string
*/
public $keyType = 'pem';
/**
* 包含 SSL 私钥的文件名.
*
* @var string
*/
public $keyPath = '';
/**
* SSL私钥的密码
*
* @var string
*/
public $keyPassword = null;
/**
* 请求方法.
*
* @var string
*/
public $method = 'GET';
/**
* Http 协议版本.
*
* @var string
*/
public $protocolVersion = '1.1';
/**
* 是否启用连接池,默认为 null 时取全局设置.
*
* @var bool|null
*/
public $connectionPool = null;
/**
* 代理认证方式.
*
* @var array
*/
public static $proxyAuths = [];
/**
* 代理类型.
*
* @var array
*/
public static $proxyType = [];
/**
* 自动扩展名标志.
*/
const AUTO_EXT_FLAG = '.*';
/**
* 自动扩展名用的临时文件名.
*/
const AUTO_EXT_TEMP_EXT = '.tmp';
/**
* 构造方法.
*
* @return mixed
*/
public function __construct()
{
$this->open();
}
/**
* 析构方法.
*/
public function __destruct()
{
$this->close();
}
/**
* 打开一个新连接,初始化所有参数。一般不需要手动调用。
*
* @return void
*/
public function open()
{
$this->handler = YurunHttp::getHandler();
$this->retry = 0;
$this->headers = $this->options = [];
$this->url = $this->content = '';
$this->useProxy = false;
$this->proxy = [
'auth' => 'basic',
'type' => 'http',
];
$this->isVerifyCA = false;
$this->caCert = null;
$this->connectTimeout = 30000;
$this->timeout = 30000;
$this->downloadSpeed = null;
$this->uploadSpeed = null;
$this->username = null;
$this->password = null;
$this->saveFileOption = [];
}
/**
* 关闭连接。一般不需要手动调用。
*
* @return void
*/
public function close()
{
if ($this->handler)
{
$handler = $this->handler;
$this->handler = null;
$handler->close();
}
}
/**
* 创建一个新会话,等同于new.
*
* @return static
*/
public static function newSession()
{
return new static();
}
/**
* 获取处理器.
*
* @return \Yurun\Util\YurunHttp\Handler\IHandler|null
*/
public function getHandler()
{
return $this->handler;
}
/**
* 设置请求地址
*
* @param string $url 请求地址
*
* @return static
*/
public function url($url)
{
$this->url = $url;
return $this;
}
/**
* 设置发送内容,requestBody的别名.
*
* @param mixed $content 发送内容,可以是字符串、数组
*
* @return static
*/
public function content($content)
{
return $this->requestBody($content);
}
/**
* 设置参数,requestBody的别名.
*
* @param mixed $params 发送内容,可以是字符串、数组
*
* @return static
*/
public function params($params)
{
return $this->requestBody($params);
}
/**
* 设置请求主体.
*
* @param string|string|array $requestBody 发送内容,可以是字符串、数组
*
* @return static
*/
public function requestBody($requestBody)
{
$this->content = $requestBody;
return $this;
}
/**
* 批量设置CURL的Option.
*
* @param array $options curl_setopt_array()所需要的第二个参数
*
* @return static
*/
public function options($options)
{
$thisOptions = &$this->options;
foreach ($options as $key => $value)
{
$thisOptions[$key] = $value;
}
return $this;
}
/**
* 设置CURL的Option.
*
* @param int $option 需要设置的CURLOPT_XXX选项
* @param mixed $value 值
*
* @return static
*/
public function option($option, $value)
{
$this->options[$option] = $value;
return $this;
}
/**
* 批量设置请求头.
*
* @param array $headers 键值数组
*
* @return static
*/
public function headers($headers)
{
$thisHeaders = &$this->headers;
$thisHeaders = array_merge($thisHeaders, $headers);
return $this;
}
/**
* 设置请求头.
*
* @param string $header 请求头名称
* @param string $value 值
*
* @return static
*/
public function header($header, $value)
{
$this->headers[$header] = $value;
return $this;
}
/**
* 批量设置请求头,.
*
* @param array $headers 纯文本 header 数组
*
* @return static
*/
public function rawHeaders($headers)
{
$thisHeaders = &$this->headers;
foreach ($headers as $header)
{
$list = explode(':', $header, 2);
$thisHeaders[trim($list[0])] = trim($list[1]);
}
return $this;
}
/**
* 设置请求头.
*
* @param string $header 纯文本 header
*
* @return static
*/
public function rawHeader($header)
{
$list = explode(':', $header, 2);
$this->headers[trim($list[0])] = trim($list[1]);
return $this;
}
/**
* 设置Accept.
*
* @param string $accept
*
* @return static
*/
public function accept($accept)
{
$this->headers['Accept'] = $accept;
return $this;
}
/**
* 设置Accept-Language.
*
* @param string $acceptLanguage
*
* @return static
*/
public function acceptLanguage($acceptLanguage)
{
$this->headers['Accept-Language'] = $acceptLanguage;
return $this;
}
/**
* 设置Accept-Encoding.
*
* @param string $acceptEncoding
*
* @return static
*/
public function acceptEncoding($acceptEncoding)
{
$this->headers['Accept-Encoding'] = $acceptEncoding;
return $this;
}
/**
* 设置Accept-Ranges.
*
* @param string $acceptRanges
*
* @return static
*/
public function acceptRanges($acceptRanges)
{
$this->headers['Accept-Ranges'] = $acceptRanges;
return $this;
}
/**
* 设置Cache-Control.
*
* @param string $cacheControl
*
* @return static
*/
public function cacheControl($cacheControl)
{
$this->headers['Cache-Control'] = $cacheControl;
return $this;
}
/**
* 批量设置Cookies.
*
* @param array $cookies 键值对应数组
*
* @return static
*/
public function cookies($cookies)
{
$this->cookies = array_merge($this->cookies, $cookies);
return $this;
}
/**
* 设置Cookie.
*
* @param string $name 名称
* @param string $value 值
*
* @return static
*/
public function cookie($name, $value)
{
$this->cookies[$name] = $value;
return $this;
}
/**
* 设置Content-Type.
*
* @param string $contentType
*
* @return static
*/
public function contentType($contentType)
{
$this->headers['Content-Type'] = $contentType;
return $this;
}
/**
* 设置Range.
*
* @param string $range
*
* @return static
*/
public function range($range)
{
$this->headers['Range'] = $range;
return $this;
}
/**
* 设置Referer.
*
* @param string $referer
*
* @return static
*/
public function referer($referer)
{
$this->headers['Referer'] = $referer;
return $this;
}
/**
* 设置User-Agent.
*
* @param string $userAgent
*
* @return static
*/
public function userAgent($userAgent)
{
$this->headers['User-Agent'] = $userAgent;
return $this;
}
/**
* 设置User-Agent,userAgent的别名.
*
* @param string $userAgent
*
* @return static
*/
public function ua($userAgent)
{
return $this->userAgent($userAgent);
}
/**
* 设置失败重试次数,状态码为5XX或者0才需要重试.
*
* @param string $retry
*
* @return static
*/
public function retry($retry)
{
$this->retry = $retry < 0 ? 0 : $retry; //至少请求1次,即重试0次
return $this;
}
/**
* 代理.
*
* @param string $server 代理服务器地址
* @param int $port 代理服务器端口
* @param string $type 代理类型,支持:http、socks4、socks4a、socks5
* @param string $auth 代理认证方式,支持:basic、ntlm。一般默认basic
*
* @return static
*/
public function proxy($server, $port, $type = 'http', $auth = 'basic')
{
$this->useProxy = true;
$this->proxy = [
'server' => $server,
'port' => $port,
'type' => $type,
'auth' => $auth,
];
return $this;
}
/**
* 代理认证
*
* @param string $username
* @param string $password
*
* @return static
*/
public function proxyAuth($username, $password)
{
$this->proxy['username'] = $username;
$this->proxy['password'] = $password;
return $this;
}
/**
* 设置超时时间.
*
* @param int $timeout 总超时时间,单位:毫秒
* @param int $connectTimeout 连接超时时间,单位:毫秒
*
* @return static
*/
public function timeout($timeout = null, $connectTimeout = null)
{
if (null !== $timeout)
{
$this->timeout = $timeout;
}
if (null !== $connectTimeout)
{
$this->connectTimeout = $connectTimeout;
}
return $this;
}
/**
* 限速
*
* @param int $download 下载速度,为0则不限制,单位:字节
* @param int $upload 上传速度,为0则不限制,单位:字节
*
* @return static
*/
public function limitRate($download = 0, $upload = 0)
{
$this->downloadSpeed = $download;
$this->uploadSpeed = $upload;
return $this;
}
/**
* 设置用于连接中需要的用户名和密码
*
* @param string $username 用户名
* @param string $password 密码
*
* @return static
*/
public function userPwd($username, $password)
{
$this->username = $username;
$this->password = $password;
return $this;
}
/**
* 保存至文件的设置.
*
* @param string $filePath 文件路径
* @param string $fileMode 文件打开方式,默认w+
*
* @return static
*/
public function saveFile($filePath, $fileMode = 'w+')
{
$this->saveFileOption['filePath'] = $filePath;
$this->saveFileOption['fileMode'] = $fileMode;
return $this;
}
/**
* 获取文件保存路径.
*
* @return string|null
*/
public function getSavePath()
{
$saveFileOption = $this->saveFileOption;
return isset($saveFileOption['filePath']) ? $saveFileOption['filePath'] : null;
}
/**
* 设置SSL证书.
*
* @param string $path 一个包含 PEM 格式证书的文件名
* @param string $type 证书类型,支持的格式有"PEM"(默认值),"DER"和"ENG"
* @param string $password 使用证书需要的密码
*
* @return static
*/
public function sslCert($path, $type = null, $password = null)
{
$this->certPath = $path;
if (null !== $type)
{
$this->certType = $type;
}
if (null !== $password)
{
$this->certPassword = $password;
}
return $this;
}
/**
* 设置SSL私钥.
*
* @param string $path 包含 SSL 私钥的文件名
* @param string $type certType规定的私钥的加密类型,支持的密钥类型为"PEM"(默认值)、"DER"和"ENG"
* @param string $password SSL私钥的密码
*
* @return static
*/
public function sslKey($path, $type = null, $password = null)
{
$this->keyPath = $path;
if (null !== $type)
{
$this->keyType = $type;
}
if (null !== $password)
{
$this->keyPassword = $password;
}
return $this;
}
/**
* 设置请求方法.
*
* @param string $method
*
* @return static
*/
public function method($method)
{
$this->method = $method;
return $this;
}
/**
* 设置是否启用连接池.
*
* @param bool $connectionPool
*
* @return static
*/
public function connectionPool($connectionPool)
{
$this->connectionPool = $connectionPool;
return $this;
}
/**
* 处理请求主体.
*
* @param string|string|array $requestBody
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return array
*/
protected function parseRequestBody($requestBody, $contentType)
{
$body = $files = [];
if (\is_string($requestBody))
{
$body = $requestBody;
}
elseif (\is_array($requestBody))
{
switch ($contentType)
{
case 'json':
$body = json_encode($requestBody);
$this->header('Content-Type', MediaType::APPLICATION_JSON);
break;
default:
foreach ($requestBody as $k => $v)
{
if ($v instanceof UploadedFile)
{
$files[$k] = $v;
}
else
{
$body[$k] = $v;
}
}
$body = http_build_query($body, '', '&');
}
}
else
{
throw new \InvalidArgumentException('$requestBody only can be string or array');
}
return [$body, $files];
}
/**
* 构建请求类.
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string|null $method 请求方法,GET、POST等
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Request
*/
public function buildRequest($url = null, $requestBody = null, $method = null, $contentType = null)
{
if (null === $url)
{
$url = $this->url;
}
if (null === $method)
{
$method = $this->method;
}
list($body, $files) = $this->parseRequestBody(null === $requestBody ? $this->content : $requestBody, $contentType);
$request = new Request($url, $this->headers, $body, $method);
$saveFileOption = $this->saveFileOption;
$request = $request->withUploadedFiles($files)
->withCookieParams($this->cookies)
->withAttribute(Attributes::MAX_REDIRECTS, $this->maxRedirects)
->withAttribute(Attributes::IS_VERIFY_CA, $this->isVerifyCA)
->withAttribute(Attributes::CA_CERT, $this->caCert)
->withAttribute(Attributes::CERT_PATH, $this->certPath)
->withAttribute(Attributes::CERT_PASSWORD, $this->certPassword)
->withAttribute(Attributes::CERT_TYPE, $this->certType)
->withAttribute(Attributes::KEY_PATH, $this->keyPath)
->withAttribute(Attributes::KEY_PASSWORD, $this->keyPassword)
->withAttribute(Attributes::KEY_TYPE, $this->keyType)
->withAttribute(Attributes::OPTIONS, $this->options)
->withAttribute(Attributes::SAVE_FILE_PATH, isset($saveFileOption['filePath']) ? $saveFileOption['filePath'] : null)
->withAttribute(Attributes::USE_PROXY, $this->useProxy)
->withAttribute(Attributes::USERNAME, $this->username)
->withAttribute(Attributes::PASSWORD, $this->password)
->withAttribute(Attributes::CONNECT_TIMEOUT, $this->connectTimeout)
->withAttribute(Attributes::TIMEOUT, $this->timeout)
->withAttribute(Attributes::DOWNLOAD_SPEED, $this->downloadSpeed)
->withAttribute(Attributes::UPLOAD_SPEED, $this->uploadSpeed)
->withAttribute(Attributes::FOLLOW_LOCATION, $this->followLocation)
->withAttribute(Attributes::CONNECTION_POOL, $this->connectionPool)
->withProtocolVersion($this->protocolVersion)
;
foreach ($this->proxy as $name => $value)
{
$request = $request->withAttribute('proxy.' . $name, $value);
}
return $request;
}
/**
* 发送请求,所有请求的老祖宗.
*
* @param string|null $url 请求地址,如果为null则取url属性值
* @param string|array|null $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string $method 请求方法,GET、POST等
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function send($url = null, $requestBody = null, $method = null, $contentType = null)
{
$request = $this->buildRequest($url, $requestBody, $method, $contentType);
return YurunHttp::send($request, $this->handler);
}
/**
* 发送 Http2 请求不调用 recv().
*
* @param string|null $url 请求地址,如果为null则取url属性值
* @param string|array|null $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string $method 请求方法,GET、POST等
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function sendHttp2WithoutRecv($url = null, $requestBody = null, $method = 'GET', $contentType = null)
{
$request = $this->buildRequest($url, $requestBody, $method, $contentType)
->withProtocolVersion('2.0')
->withAttribute(Attributes::HTTP2_NOT_RECV, true);
return YurunHttp::send($request, $this->handler);
}
/**
* GET请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function get($url = null, $requestBody = null)
{
if (!empty($requestBody))
{
if (strpos($url, '?'))
{
$url .= '&';
}
else
{
$url .= '?';
}
$url .= http_build_query($requestBody, '', '&');
}
return $this->send($url, [], 'GET');
}
/**
* POST请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function post($url = null, $requestBody = null, $contentType = null)
{
return $this->send($url, $requestBody, 'POST', $contentType);
}
/**
* HEAD请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function head($url = null, $requestBody = null)
{
return $this->send($url, $requestBody, 'HEAD');
}
/**
* PUT请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function put($url = null, $requestBody = null, $contentType = null)
{
return $this->send($url, $requestBody, 'PUT', $contentType);
}
/**
* PATCH请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function patch($url = null, $requestBody = null, $contentType = null)
{
return $this->send($url, $requestBody, 'PATCH', $contentType);
}
/**
* DELETE请求
*
* @param string $url 请求地址,如果为null则取url属性值
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function delete($url = null, $requestBody = null, $contentType = null)
{
return $this->send($url, $requestBody, 'DELETE', $contentType);
}
/**
* 直接下载文件.
*
* @param string $fileName 保存路径,如果以 .* 结尾,则根据 Content-Type 自动决定扩展名
* @param string $url 下载文件地址
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param string $method 请求方法,GET、POST等,一般用GET
*
* @return \Yurun\Util\YurunHttp\Http\Response|null
*/
public function download($fileName, $url = null, $requestBody = null, $method = 'GET')
{
$isAutoExt = self::checkDownloadIsAutoExt($fileName, $fileName);
$result = $this->saveFile($fileName)->send($url, $requestBody, $method);
if ($isAutoExt)
{
self::parseDownloadAutoExt($result, $fileName);
}
$this->saveFileOption = [];
return $result;
}
/**
* WebSocket.
*
* @param string $url
*
* @return \Yurun\Util\YurunHttp\WebSocket\IWebSocketClient
*/
public function websocket($url = null)
{
$request = $this->buildRequest($url);
return YurunHttp::websocket($request, $this->handler);
}
/**
* 检查下载文件名是否要自动扩展名.
*
* @param string $fileName
* @param string $tempFileName
*
* @return bool
*/
public static function checkDownloadIsAutoExt($fileName, &$tempFileName)
{
$flagLength = \strlen(self::AUTO_EXT_FLAG);
if (self::AUTO_EXT_FLAG !== substr($fileName, -$flagLength))
{
return false;
}
$tempFileName = substr($fileName, 0, -$flagLength) . self::AUTO_EXT_TEMP_EXT;
return true;
}
/**
* 处理下载的自动扩展名.
*
* @param \Yurun\Util\YurunHttp\Http\Response $response
* @param string $tempFileName
*
* @return void
*/
public static function parseDownloadAutoExt(&$response, $tempFileName)
{
$ext = MediaType::getExt($response->getHeaderLine('Content-Type'));
if (null === $ext)
{
$ext = 'file';
}
$savedFileName = substr($tempFileName, 0, -\strlen(self::AUTO_EXT_TEMP_EXT)) . '.' . $ext;
rename($tempFileName, $savedFileName);
$response = $response->withSavedFileName($savedFileName);
}
}
if (\extension_loaded('curl'))
{
// 代理认证方式
HttpRequest::$proxyAuths = [
'basic' => \CURLAUTH_BASIC,
'ntlm' => \CURLAUTH_NTLM,
];
// 代理类型
HttpRequest::$proxyType = [
'http' => \CURLPROXY_HTTP,
'socks4' => \CURLPROXY_SOCKS4,
'socks4a' => 6, // CURLPROXY_SOCKS4A
'socks5' => \CURLPROXY_SOCKS5,
];
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

YurunHttp 是开源的PHP HTTP类库,支持链式操作,简单易用。3.0 版完美支持Curl、Swoole 协程。QQ群:17916227。本仓库仅用于存档,不再发布更新日志,请移步 Github:
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/serverking/YurunHttp.git
git@gitee.com:serverking/YurunHttp.git
serverking
YurunHttp
YurunHttp
dev
Going to Help Center

Search

Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /