Skip to content

Commit eacba51

Browse files
author
Wazabii
committed
Client curl init fix
Patch so curl init sooner and add get to Env
1 parent b817a5e commit eacba51

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

Client.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(array $options = [])
3030
{
3131
$this->options = $options;
3232
}
33-
33+
3434
/**
3535
* Set option
3636
* https://www.php.net/manual/en/function.curl-setopt.php
@@ -53,7 +53,7 @@ public function hasOption(int $key): bool
5353
{
5454
return (isset($this->options[$key]));
5555
}
56-
56+
5757
/**
5858
* Sends a PSR-7 request and returns a PSR-7 response.
5959
* @param RequestInterface $request
@@ -64,24 +64,19 @@ public function sendRequest(RequestInterface $request): ResponseInterface
6464
$this->requestData = (string)$request->getBody();
6565
$this->requestDataLength = strlen($this->requestData);
6666
$this->prepareRequest($request);
67-
$this->buildHeaders($request);
68-
6967
try {
68+
$this->curl = curl_init();
69+
$this->buildHeaders($request);
7070
if (!extension_loaded('curl')) {
7171
throw new InvalidArgumentException('You need to enable CURL on your server.');
7272
}
73-
7473
// Init curl request
75-
$this->curl = curl_init();
7674
$this->buildOptions();
7775
$this->buildFromMethods($request);
78-
7976
// Execute request
8077
$this->createRequest();
81-
8278
// Close curl request
8379
curl_close($this->curl);
84-
8580
// Retrive the body
8681
return $this->createResponse();
8782
} catch (InvalidArgumentException $e) {
@@ -101,22 +96,22 @@ protected function buildFromMethods(RequestInterface $request): void
10196
switch ($request->getMethod()) {
10297
case 'GET':
10398
$this->get();
104-
// no break
99+
break;
105100
case 'POST':
106101
$this->post();
107-
// no break
102+
break;
108103
case 'PUT':
109104
$this->put();
110-
// no break
105+
break;
111106
case 'PATCH':
112107
$request = $this->patch($request);
113-
// no break
108+
break;
114109
case 'DELETE':
115110
$this->delete();
116-
// no break
111+
break;
117112
default:
118-
throw new InvalidArgumentException('The requesr method (' . $request->getMethod() . ') ' .
119-
'is not supported.');
113+
throw new InvalidArgumentException('The request method (' . $request->getMethod() . ') is not supported.');
114+
break;
120115
}
121116
}
122117

@@ -241,7 +236,9 @@ private function buildHeaders(RequestInterface $request): void
241236
foreach ($request->getHeaders() as $name => $_unUsedVal) {
242237
$data[] = "{$name}: " . $request->getHeaderLine($name);
243238
}
244-
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $data);
239+
if (count($data) > 0) {
240+
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $data);
241+
}
245242
}
246243

247244
/**

Env.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public function set(string $key, string $value): string
4545
return "{$key}={$value}";
4646
}
4747

48+
public function get(string $key): string
49+
{
50+
$key = $this->formatKey($key);
51+
return $this->fileData[$key];
52+
}
53+
4854
public function drop(string $key): void
4955
{
5056
$key = $this->formatKey($key);

0 commit comments

Comments
 (0)