Skip to content

Commit 3f95b6f

Browse files
committed
feat: add support for basic auth + urlencoded data
1 parent bc4a568 commit 3f95b6f

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"minimum-stability": "dev",
3333
"prefer-stable": true,
3434
"require": {
35-
"ext-curl": "*"
35+
"ext-curl": "*",
36+
"leafs/alchemy": "dev-next"
3637
}
3738
}

src/Fetch.php

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ class Fetch
157157
// supplies credentials.
158158
// This will set an `Proxy-Authorization` header, overwriting any existing
159159
// `Proxy-Authorization` custom headers you have set using `headers`.
160-
// If the proxy server uses HTTPS, then you must set the protocol to `https`.
160+
// If the proxy server uses HTTPS, then you must set the protocol to `https`.
161161
"proxy" => [],
162162

163-
// `decompress` indicates whether or not the response body should be decompressed
164-
// automatically. If set to `true` will also remove the 'content-encoding' header
163+
// `decompress` indicates whether or not the response body should be decompressed
164+
// automatically. If set to `true` will also remove the 'content-encoding' header
165165
// from the responses objects of all decompressed responses
166166
// - Node only (XHR cannot turn off decompression)
167167
"decompress" => true, // default
@@ -256,18 +256,31 @@ private static function call($request)
256256
{
257257
static::$handler = curl_init();
258258

259+
$curl_base_options = [
260+
CURLOPT_RETURNTRANSFER => true,
261+
CURLOPT_FOLLOWLOCATION => true,
262+
CURLOPT_MAXREDIRS => $request["maxRedirects"],
263+
CURLOPT_HTTPHEADER => $request["headers"],
264+
CURLOPT_HEADER => true,
265+
CURLOPT_SSL_VERIFYPEER => $request["verifyPeer"],
266+
CURLOPT_SSL_VERIFYHOST => $request["verifyHost"] === false ? 0 : 2,
267+
// If an empty string, '', is set, a header containing all supported encoding types is sent
268+
CURLOPT_ENCODING => ""
269+
];
270+
259271
if ($request["method"] !== static::GET) {
260272
if ($request["method"] === static::POST) {
261-
curl_setopt(static::$handler, CURLOPT_POST, true);
273+
$curl_base_options[CURLOPT_POST] = true;
262274
} else {
263275
if ($request["method"] === static::HEAD) {
264-
curl_setopt(static::$handler, CURLOPT_NOBODY, true);
276+
$curl_base_options[CURLOPT_NOBODY] = true;
265277
}
266-
curl_setopt(static::$handler, CURLOPT_CUSTOMREQUEST, $request["method"]);
278+
279+
$curl_base_options[CURLOPT_CUSTOMREQUEST] = $request["method"];
267280
}
268281

269-
curl_setopt(static::$handler, CURLOPT_POSTFIELDS, $request["data"]);
270-
} else if (is_array($request["data"])) {
282+
$curl_base_options[CURLOPT_POSTFIELDS] = $request["data"];
283+
} else if (\is_array($request["data"])) {
271284
if (strpos($request["url"], '?') !== false) {
272285
$request["url"] .= '&';
273286
} else {
@@ -277,23 +290,26 @@ private static function call($request)
277290
$request["url"] .= urldecode(http_build_query(self::buildHTTPCurlQuery($request["data"])));
278291
}
279292

280-
$curl_base_options = [
281-
CURLOPT_URL => $request["baseUrl"] . $request["url"],
282-
CURLOPT_RETURNTRANSFER => true,
283-
CURLOPT_FOLLOWLOCATION => true,
284-
CURLOPT_MAXREDIRS => $request["maxRedirects"],
285-
CURLOPT_HTTPHEADER => $request["headers"],
286-
CURLOPT_HEADER => true,
287-
CURLOPT_SSL_VERIFYPEER => $request["verifyPeer"],
288-
CURLOPT_SSL_VERIFYHOST => $request["verifyHost"] === false ? 0 : 2,
289-
// If an empty string, '', is set, a header containing all supported encoding types is sent
290-
CURLOPT_ENCODING => ""
291-
];
293+
$curl_base_options[CURLOPT_URL] = $request["baseUrl"] . $request["url"];
294+
295+
// supporting deprecated http auth method
296+
if (!empty($request['auth'] ?? [])) {
297+
// $curl_base_options = \array_merge($curl_base_options, [
298+
// CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
299+
// CURLOPT_USERPWD => $request['auth']['username'] . ':' . $request['auth']['password']
300+
// ]);
301+
$curl_base_options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
302+
$curl_base_options[CURLOPT_USERPWD] = $request['auth']['username'] . ':' . $request['auth']['password'];
303+
}
292304

293305
foreach ($request["curl"] as $key => $value) {
294306
$curl_base_options[$key] = $value;
295307
}
296308

309+
if (($request['headers']['Content-Type'] ?? null) === 'application/x-www-form-urlencoded' && $request['method'] !== static::GET) {
310+
$curl_base_options[CURLOPT_POSTFIELDS] = http_build_query($request['data']);
311+
}
312+
297313
curl_setopt_array(static::$handler, $curl_base_options);
298314

299315
if ($request["timeout"] !== null) {
@@ -309,14 +325,6 @@ private static function call($request)
309325
// curl_setopt(static::$handler, CURLOPT_COOKIEJAR, self::$cookieFile);
310326
// }
311327

312-
// supporting deprecated http auth method
313-
// if (!empty($username)) {
314-
// curl_setopt_array(static::$handler, array(
315-
// CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
316-
// CURLOPT_USERPWD => $username . ':' . $password
317-
// ));
318-
// }
319-
320328
// if (!empty($request["auth"]["user"])) {
321329
// curl_setopt_array(static::$handler, [
322330
// CURLOPT_HTTPAUTH => $request["auth"]["method"],
@@ -335,19 +343,26 @@ private static function call($request)
335343
// ]);
336344
// }
337345

338-
$response = curl_exec(static::$handler);
339-
$error = curl_error(static::$handler);
340-
$info = self::getInfo();
346+
// debug curl, get built request as a dump
347+
// response()->json([
348+
// 'curl_request' => curl_getinfo(static::$handler),
349+
// 'options' => $curl_base_options,
350+
// ]);
351+
// die;
352+
353+
$response = curl_exec(static::$handler);
354+
$error = curl_error(static::$handler);
355+
$info = self::getInfo();
341356

342357
if ($error) {
343358
throw new \Exception($error);
344359
}
345360

346361
// Split the full response in its headers and body
347362
$header_size = $info['header_size'];
348-
$header = substr($response, 0, $header_size);
349-
$body = substr($response, $header_size);
350-
$httpCode = $info['http_code'];
363+
$header = substr($response, 0, $header_size);
364+
$body = substr($response, $header_size);
365+
$httpCode = $info['http_code'];
351366

352367
if (!$request["rawResponse"]) {
353368
$body = json_decode($body);
@@ -379,7 +394,7 @@ private static function call($request)
379394
* http://php.net/manual/en/function.http-parse-headers.php#112986
380395
* @param string $raw_headers raw headers
381396
* @return array
382-
*
397+
*
383398
* @author Mashape (https://www.mashape.com)
384399
*/
385400
private static function parseHeaders(string $raw_headers): array
@@ -432,7 +447,7 @@ public static function getInfo($opt = false)
432447
* @param array $data array to flatten.
433448
* @param bool|string $parent parent key or false if no parent
434449
* @return array
435-
*
450+
*
436451
* @author Mashape (https://www.mashape.com)
437452
*/
438453
public static function buildHTTPCurlQuery(array $data, $parent = false): array

0 commit comments

Comments
 (0)