Skip to content

Commit 4cfd9e1

Browse files
committed
up: fix stream and fscock client send request error
1 parent 5fbeb78 commit 4cfd9e1

15 files changed

+143
-123
lines changed

src/AbstractClient.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ abstract class AbstractClient implements ClientInterface
5757
// retry times, when an error occurred.
5858
'retry' => 3,
5959
'method' => 'GET', // 'POST'
60+
'version' => '1.1', // http version
6061
'baseUrl' => '',
61-
'timeout' => 5,
62+
'timeout' => 5, // seconds
6263
// enable SSL verify
6364
'sslVerify' => false,
6465
// request headers
@@ -123,6 +124,11 @@ abstract class AbstractClient implements ClientInterface
123124
*/
124125
protected array $cookies = [];
125126

127+
/**
128+
* @var array Record debug info on $options['debug'] = true
129+
*/
130+
private array $_debugInfo = [];
131+
126132
/**************************************************************************
127133
* response data
128134
*************************************************************************/
@@ -186,11 +192,9 @@ public static function driverName(): string
186192
}
187193

188194
/**
189-
* SimpleCurl constructor.
190-
*
191-
* @param array $options
195+
* Class constructor.
192196
*
193-
* @throws RuntimeException
197+
* @param array $options = self::$defaultOptions
194198
*/
195199
public function __construct(array $options = [])
196200
{
@@ -692,12 +696,15 @@ protected function resetOptions(): static
692696
}
693697

694698
/**
699+
* reset request: headers, cookies, debugInfo
700+
*
695701
* @return $this
696702
*/
697703
public function resetRequest(): static
698704
{
699-
$this->headers = [];
700-
$this->cookies = [];
705+
$this->headers = $this->cookies = [];
706+
707+
$this->_debugInfo = [];
701708
return $this;
702709
}
703710

@@ -729,10 +736,20 @@ public function resetResponse(): static
729736
return $this;
730737
}
731738

739+
/**
740+
* Reset the request and response info.
741+
*
742+
* @return static
743+
*/
744+
public function resetRuntime(): static
745+
{
746+
return $this->resetRequest()->resetResponse();
747+
}
748+
732749
/**
733750
* Reset the last time headers,cookies,options,response data.
734751
*
735-
* @return $this
752+
* @return static
736753
*/
737754
public function reset(): static
738755
{
@@ -844,6 +861,24 @@ public function setDebug(mixed $debug): static
844861
return $this;
845862
}
846863

864+
/**
865+
* add debug info
866+
*/
867+
public function addDebugInfo(string $key, mixed $value): void
868+
{
869+
$this->_debugInfo[$key] = $value;
870+
}
871+
872+
/**
873+
* Get debug info on options.debug=true
874+
*
875+
* @return array
876+
*/
877+
public function getDebugInfo(): array
878+
{
879+
return $this->_debugInfo;
880+
}
881+
847882
/**
848883
* @param int $retry
849884
*

src/Client.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
*/
3333
class Client
3434
{
35+
public const DRIVER_CURL = 'curl';
36+
public const DRIVER_FILE = 'file';
37+
public const DRIVER_FOPEN = 'fopen';
38+
public const DRIVER_FSOCK = 'fsock';
39+
public const DRIVER_STREAM = 'stream';
40+
3541
/**
3642
* The supported drivers
3743
*
@@ -77,8 +83,8 @@ public static function factory(array $config): AbstractClient
7783
$name = $config['driver'] ?? '';
7884
$class = self::$drivers[$name] ?? '';
7985

86+
// auto select driver
8087
if (!$class) {
81-
// auto select
8288
foreach (self::$drivers as $driverClass) {
8389
if ($driverClass::isAvailable()) {
8490
$class = $driverClass;
@@ -125,7 +131,7 @@ public static function setDefaultDriver(ClientInterface $defaultDriver): void
125131

126132
/**
127133
* @param string $method
128-
* @param array $args
134+
* @param array $args
129135
*
130136
* @return AbstractClient
131137
*/
@@ -137,7 +143,7 @@ public static function __callStatic(string $method, array $args)
137143
}
138144

139145
if (method_exists($client, $method)) {
140-
return $client->reset()->$method(...$args);
146+
return $client->resetRuntime()->$method(...$args);
141147
}
142148

143149
throw new InvalidArgumentException('call invalid class method: ' . $method);

src/ClientConst.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
class ClientConst
1111
{
12+
public const CONTENT_TYPE = 'Content-Type';
13+
public const USERAGENT = 'User-Agent';
14+
1215
public const HTML = 'text/html';
1316
public const JSON = 'application/json';
1417
public const XML = 'application/xml';

src/ClientInterface.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,28 @@ public function request(
210210
public function setResponseCreator(callable $responseCreator): static;
211211

212212
/**
213-
* reset options, request headers, cookies, response data...
213+
* reset all: options, request headers, cookies, response data...
214214
*
215215
* @return static
216216
*/
217217
public function reset(): static;
218218

219219
/**
220-
* Reset request data
220+
* Reset the request and response info.
221+
*
222+
* @return static
223+
*/
224+
public function resetRuntime(): static;
225+
226+
/**
227+
* Reset request data: headers, cookies, debugInfo
221228
*
222229
* @return static
223230
*/
224231
public function resetRequest(): static;
225232

226233
/**
227-
* Reset response data
234+
* Reset response data:
228235
*
229236
* @return static
230237
*/

src/Curl/CurlClient.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,28 @@ class CurlClient extends AbstractClient implements CurlClientInterface
164164
* save request and response info, data from curl_getinfo()
165165
*
166166
* @link https://secure.php.net/manual/zh/function.curl-getinfo.php
167-
* contains key:
168-
* "url"
169-
* "content_type"
170-
* "http_code"
171-
* "header_size"
172-
* "request_size"
173-
* "filetime"
174-
* "ssl_verify_result"
175-
* "redirect_count"
176-
* "total_time"
177-
* "namelookup_time"
178-
* "connect_time"
179-
* "pretransfer_time"
180-
* "size_upload"
181-
* "size_download"
182-
* "speed_download"
183-
* "speed_upload"
184-
* "download_content_length"
185-
* "upload_content_length"
186-
* "starttransfer_time"
187-
* "redirect_time"
167+
* @var array = [
168+
* "url" => '',
169+
* "content_type" => '',
170+
* "http_code" => 400,
171+
* "header_size" => '',
172+
* "request_size" => '',
173+
* "filetime" => '',
174+
* "ssl_verify_result" => '',
175+
* "redirect_count" => '',
176+
* "total_time" => '',
177+
* "namelookup_time" => '',
178+
* "connect_time" => '',
179+
* "pretransfer_time" => '',
180+
* "size_upload" => '',
181+
* "size_download" => '',
182+
* "speed_download" => '',
183+
* "speed_upload" => '',
184+
* "download_content_length" => '',
185+
* "upload_content_length" => '',
186+
* "starttransfer_time" => '',
187+
* "redirect_time" => '',
188+
* ]
188189
*/
189190
private array $_responseInfo = [];
190191

src/FOpenClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ public function request(
111111
// set timeout
112112
stream_set_timeout($this->handle, (int)$options['timeout']);
113113

114+
if ($this->isDebug()) {
115+
$this->addDebugInfo('url', $url);
116+
$this->addDebugInfo('options', $options);
117+
$this->addDebugInfo('data', $data);
118+
}
119+
114120
// read response
115121
// $content = \stream_get_contents($this->handle);
116122
while (!feof($this->handle)) {

src/FSockClient.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,36 @@ public function request(
101101
throw new ClientException($error, $errno);
102102
}
103103

104-
$string = $this->buildRawHttpData($info, $headers, $options, $data);
104+
$this->buildRequestAndWrite($handle, $info, $headers, $options, $data);
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* @param resource $handle
111+
* @param array $info
112+
* @param array $headers
113+
* @param array $options
114+
* @param mixed $data
115+
*
116+
* @return void
117+
*/
118+
protected function buildRequestAndWrite($handle, array $info, array $headers, array $options, mixed $data): void
119+
{
120+
$timeout = (int)$options['timeout'];
121+
$request = $this->buildRawHttpData($info, $headers, $options, $data);
122+
123+
if ($this->isDebug()) {
124+
$this->addDebugInfo('urlInfo', $info);
125+
$this->addDebugInfo('options', $options);
126+
$this->addDebugInfo('request', $request);
127+
}
105128

106129
// set timeout
107130
stream_set_timeout($handle, $timeout);
108131

109132
// send request
110-
if (false === fwrite($handle, $string)) {
133+
if (false === fwrite($handle, $request)) {
111134
throw new RequestException('send request to server is fail');
112135
}
113136

@@ -123,8 +146,6 @@ public function request(
123146

124147
// parse raw response
125148
$this->parseResponse();
126-
127-
return $this;
128149
}
129150

130151
/**

src/FileClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function request(
6262
// merge global options data.
6363
$options = array_merge($this->options, $options);
6464

65+
if ($this->isDebug()) {
66+
$this->addDebugInfo('url', $url);
67+
$this->addDebugInfo('options', $options);
68+
$this->addDebugInfo('data', $data);
69+
}
70+
6571
try {
6672
$reqCtx = $this->buildStreamContext($url, $headers, $options, $data);
6773
$fullUrl = UrlHelper::encode2($this->fullUrl);

0 commit comments

Comments
 (0)