Skip to content

Commit c0f5d70

Browse files
committed
format codes. update action script
1 parent 25c0406 commit c0f5d70

19 files changed

+115
-69
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
with:
3030
php-version: ${{ matrix.php}}
3131
tools: pecl, php-cs-fixer, phpunit:8.5.8
32-
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis # , swoole-4.4.19 #optional, setup extensions
32+
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis, swoole-4.5.11 # , swoole-4.4.19 #optional, setup extensions
3333
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
3434
coverage: none #optional, setup coverage driver: xdebug, none
3535

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# HTTP Client
22

33
[![License](https://img.shields.io/packagist/l/php-comp/http-client.svg?style=flat-square)](LICENSE)
4-
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/php-comp/http-client)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/php-comp/http-client)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/php-comp/http-client.svg)](https://packagist.org/packages/php-comp/http-client)
6+
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/ulue/phpgit)](https://github.com/ulue/phpgit)
7+
[![Github Actions Status](https://github.com/ulue/phpgit/workflows/Unit-tests/badge.svg)](https://github.com/ulue/phpgit/actions)
68

79
PHP http client library.
810

@@ -22,6 +24,8 @@ composer require php-comp/http-client
2224
### 创建客户端实例
2325

2426
```php
27+
use PhpComp\Http\Client\Client;
28+
2529
// use factory
2630
$client = Client::factory([
2731
'driver' => 'curl', // stream, fsock, fopen, file, co, co2
@@ -44,6 +48,10 @@ $client = CoClient::create($options);
4448
### 基本使用
4549

4650
```php
51+
use PhpComp\Http\Client\Client;
52+
53+
$client = Client::factory([]);
54+
4755
$client->get('/users/1');
4856

4957
$post = ['name' => 'john'];
@@ -53,6 +61,8 @@ $client->post('/users/1', $post);
5361
$client->byAjax()->post('/users/1', $post);
5462

5563
// add json content type
64+
$client->json('/users/1', json_encode($post));
65+
// or
5666
$client->byJson()->post('/users/1', json_encode($post));
5767

5868
$statusCode = $client->getStatusCode();

example/refer.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php declare(strict_types=1);
22
/**
33
* # example 1 获取一个页面并发送 POST 数据
4+
*
45
* @link https://secure.php.net/manual/zh/context.http.php#refsect1-context.http-examples
56
*/
67

@@ -11,37 +12,40 @@
1112
]
1213
);
1314

14-
$opts = ['http' =>
15-
[
16-
'method' => 'POST',
17-
'header' => 'Content-type: application/x-www-form-urlencoded',
18-
'content' => $postdata
19-
]
15+
$opts = [
16+
'http' =>
17+
[
18+
'method' => 'POST',
19+
'header' => 'Content-type: application/x-www-form-urlencoded',
20+
'content' => $postdata
21+
]
2022
];
2123

2224
$context = stream_context_create($opts);
23-
$result = file_get_contents('http://example.com/submit.php', false, $context);
25+
$result = file_get_contents('http://example.com/submit.php', false, $context);
2426

2527
?>
2628

2729
<?php
2830
/**
2931
* # example 2 忽略重定向并获取 header 和内容
32+
*
3033
* @link https://secure.php.net/manual/zh/context.http.php#refsect1-context.http-examples
3134
*/
3235

3336
$url = 'http://www.example.org/header.php';
3437

35-
$opts = ['http' =>
36-
[
37-
'method' => 'GET',
38-
'max_redirects' => '0',
39-
'ignore_errors' => '1'
40-
]
38+
$opts = [
39+
'http' =>
40+
[
41+
'method' => 'GET',
42+
'max_redirects' => '0',
43+
'ignore_errors' => '1'
44+
]
4145
];
4246

4347
$context = stream_context_create($opts);
44-
$stream = fopen($url, 'r', false, $context);
48+
$stream = fopen($url, 'r', false, $context);
4549

4650
// header information as well as meta data
4751
// about the stream
@@ -61,7 +65,7 @@
6165
$data = file_get_contents('http://www.example.com/', null, stream_context_create([
6266
'http' => [
6367
'protocol_version' => 1.1,
64-
'header' => [
68+
'header' => [
6569
'Connection: close',
6670
],
6771
],
@@ -73,12 +77,15 @@
7377
* @link https://secure.php.net/manual/zh/context.http.php#110449
7478
*/
7579

76-
$stream = stream_context_create(['http' => ['method' => 'GET',
77-
'timeout' => 20,
78-
'header' => 'User-agent: Myagent',
79-
'proxy' => 'tcp://my-proxy.localnet:3128',
80-
'request_fulluri' => true /* without this option we get an HTTP error! */
81-
]]);
80+
$stream = stream_context_create([
81+
'http' => [
82+
'method' => 'GET',
83+
'timeout' => 20,
84+
'header' => 'User-agent: Myagent',
85+
'proxy' => 'tcp://my-proxy.localnet:3128',
86+
'request_fulluri' => true /* without this option we get an HTTP error! */
87+
]
88+
]);
8289

8390
if ($fp = fopen('http://example.com', 'r', false, $stream)) {
8491
print 'well done';

src/AbstractClient.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace PhpComp\Http\Client;
1111

1212
use Closure;
13-
use Exception;
1413
use InvalidArgumentException;
1514
use Psr\Http\Message\RequestInterface;
1615
use Psr\Http\Message\ResponseInterface;
@@ -208,7 +207,7 @@ public function __destruct()
208207
/**
209208
* @return array
210209
*/
211-
public static function getSupportedMethods()
210+
public static function getSupportedMethods(): array
212211
{
213212
return self::$supportedMethods;
214213
}
@@ -308,8 +307,6 @@ public function trace(string $url, $params = null, array $headers = [], array $o
308307
* @param RequestInterface $request
309308
*
310309
* @return ResponseInterface
311-
*
312-
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
313310
*/
314311
public function sendRequest(RequestInterface $request): ResponseInterface
315312
{
@@ -323,6 +320,23 @@ public function sendRequest(RequestInterface $request): ResponseInterface
323320
return $this->getPsr7Response();
324321
}
325322

323+
/**
324+
* @param string $url
325+
* @param mixed $data
326+
* @param array $headers
327+
* @param array $options
328+
*
329+
* @return ClientInterface
330+
*/
331+
public function json(string $url, $data = null, array $headers = [], array $options = []): ClientInterface
332+
{
333+
if (!isset($options['method'])) {
334+
$options['method'] = 'POST';
335+
}
336+
337+
return $this->byJson()->request($url, $data, $options['method'], $headers, $options);
338+
}
339+
326340
/**
327341
* File download and save
328342
*
@@ -363,7 +377,7 @@ public function getTimeout(): int
363377
*
364378
* @return $this
365379
*/
366-
public function setTimeout(int $seconds)
380+
public function setTimeout(int $seconds): self
367381
{
368382
$this->options['timeout'] = $seconds;
369383
return $this;
@@ -374,7 +388,7 @@ public function setTimeout(int $seconds)
374388
*
375389
* @return $this
376390
*/
377-
public function SSLVerify(bool $enable)
391+
public function SSLVerify(bool $enable): self
378392
{
379393
$this->options['sslVerify'] = $enable;
380394
return $this;
@@ -435,7 +449,7 @@ public function acceptGzip()
435449
/**
436450
* @return $this
437451
*/
438-
public function byJson()
452+
public function byJson(): self
439453
{
440454
$this->setHeader('Content-Type', 'application/json; charset=utf-8');
441455
return $this;
@@ -444,15 +458,15 @@ public function byJson()
444458
/**
445459
* @return $this
446460
*/
447-
public function byXhr()
461+
public function byXhr(): self
448462
{
449463
return $this->byAjax();
450464
}
451465

452466
/**
453467
* @return $this
454468
*/
455-
public function byAjax()
469+
public function byAjax(): self
456470
{
457471
$this->setHeader('X-Requested-With', 'XMLHttpRequest');
458472
return $this;
@@ -478,7 +492,7 @@ public function setUserAgent(string $userAgent): ClientInterface
478492
*
479493
* @return $this
480494
*/
481-
public function setUserAuth(string $user, string $pwd = '', int $authType = self::AUTH_BASIC)
495+
public function setUserAuth(string $user, string $pwd = '', int $authType = self::AUTH_BASIC): AbstractClient
482496
{
483497
if ($authType === self::AUTH_BASIC) {
484498
$sign = 'Basic ' . base64_encode("$user:$pwd");
@@ -613,7 +627,7 @@ public function delHeader($names): ClientInterface
613627
*
614628
* @return string
615629
*/
616-
protected function buildFullUrl(string $url, $data = null)
630+
protected function buildFullUrl(string $url, $data = null): string
617631
{
618632
$url = trim($url);
619633

@@ -775,7 +789,7 @@ public function getBaseUrl(): string
775789

776790
/**
777791
* @param int|string $name
778-
* @param bool $default
792+
* @param null $default
779793
*
780794
* @return mixed
781795
*/

src/CType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PhpComp\Http\Client;
4+
5+
/**
6+
* Class CType
7+
*
8+
* @package PhpComp\Http\Client
9+
*/
10+
class CType
11+
{
12+
public const HTML = 'text/html';
13+
public const JSON = 'application/json';
14+
public const XML = 'application/xml';
15+
16+
public const MULTIPART = 'multipart/form-data';
17+
}

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
class Client
3333
{
3434
/**
35-
* supported drivers
35+
* The supported drivers
3636
*
3737
* @var ClientInterface[]
3838
*/

src/ClientInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ public function trace(string $url, array $params = [], array $headers = [], arra
162162
* @param array|string|null $data
163163
* @param string $method
164164
* @param array $headers
165-
* @param array $options
165+
* @param array $options All options please {@see AbstractClient::$defaultOptions}
166166
*
167-
* @return self
167+
* @return ClientInterface
168168
*/
169169
public function request(
170170
string $url,

src/ClientUtil.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public static function parseUrl(string $url): array
103103
}
104104

105105
/**
106-
* @param string $url
107-
* @param array|object $data
106+
* @param string $url
107+
* @param null $data
108108
*
109109
* @return string
110110
*/

src/Curl/CurlClient.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,7 @@ public function upload(string $url, string $field, string $filePath, string $mim
236236
}
237237

238238
// create file
239-
if (function_exists('curl_file_create')) {
240-
$file = curl_file_create($filePath, $mimeType); // , $postFilename
241-
} else {
242-
$this->setCurlOption(CURLOPT_SAFE_UPLOAD, true);
243-
$file = "@{$filePath};type={$mimeType}"; // ;filename={$postFilename}
244-
}
239+
$file = curl_file_create($filePath, $mimeType); // , $postFilename
245240

246241
$headers = [
247242
'Content-Type' => 'multipart/form-data'
@@ -259,7 +254,7 @@ public function upload(string $url, string $field, string $filePath, string $mim
259254
*
260255
* @return string
261256
*/
262-
public function downloadImage(string $imgUrl, string $saveDir, string $rename = '')
257+
public function downloadImage(string $imgUrl, string $saveDir, string $rename = ''): string
263258
{
264259
// e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg?t=34512323
265260
if (strpos($imgUrl, '?')) {
@@ -536,7 +531,7 @@ public function setReferrer(string $referrer): self
536531
*
537532
* @return $this
538533
*/
539-
public function setUserAuth(string $user, string $pwd = '', int $authType = CURLAUTH_BASIC): ClientInterface
534+
public function setUserAuth(string $user, string $pwd = '', int $authType = CURLAUTH_BASIC): AbstractClient
540535
{
541536
$this->_curlOptions[CURLOPT_HTTPAUTH] = $authType;
542537
$this->_curlOptions[CURLOPT_USERPWD] = "$user:$pwd";
@@ -552,7 +547,7 @@ public function setUserAuth(string $user, string $pwd = '', int $authType = CURL
552547
*
553548
* @return $this
554549
*/
555-
public function setSSLAuth(string $pwd, string $file, string $authType = self::SSL_TYPE_CERT): ClientInterface
550+
public function setSSLAuth(string $pwd, string $file, string $authType = self::SSL_TYPE_CERT): AbstractClient
556551
{
557552
if ($authType !== self::SSL_TYPE_CERT && $authType !== self::SSL_TYPE_KEY) {
558553
throw new InvalidArgumentException('The SSL auth type only allow: cert|key');
@@ -653,7 +648,7 @@ public function getCurlOptions(): array
653648

654649
/**
655650
* @param int|string $name
656-
* @param bool $default
651+
* @param null $default
657652
*
658653
* @return mixed
659654
*/

src/Curl/CurlMulti.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function build(array $data): self
152152
*
153153
* @return $this
154154
*/
155-
public function append(string $url, $data = null, array $headers = [], array $options = [])
155+
public function append(string $url, $data = null, array $headers = [], array $options = []): self
156156
{
157157
$options = array_merge($this->options, $options);
158158
$this->chMap[] = $ch = $this->createResource($url, $data, $headers, $options);

0 commit comments

Comments
 (0)