modifs.mineures sans commentaires
This commit is contained in:
parent
b817ad6c98
commit
255a68315a
|
@ -12,7 +12,8 @@
|
||||||
"php": ">=7.3"
|
"php": ">=7.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"nulib/tests": "7.3"
|
"nulib/tests": "7.3",
|
||||||
|
"ext-curl": "*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "a83db90dff9c8a1e44abc608738042c3",
|
"content-hash": "f05e766a09e965a7dd63ce61d5274eab",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
|
@ -1780,6 +1780,8 @@
|
||||||
"platform": {
|
"platform": {
|
||||||
"php": ">=7.3"
|
"php": ">=7.3"
|
||||||
},
|
},
|
||||||
"platform-dev": [],
|
"platform-dev": {
|
||||||
|
"ext-curl": "*"
|
||||||
|
},
|
||||||
"plugin-api-version": "2.6.0"
|
"plugin-api-version": "2.6.0"
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
namespace nulib\web\ext;
|
||||||
|
|
||||||
|
use nulib\UserException;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class CurlException extends UserException {
|
||||||
|
function __construct($ch, ?string $message=null, $code=0, ?Throwable $previous=null) {
|
||||||
|
$user_message = $message;
|
||||||
|
if ($message === null) $message = "(unknown error)";
|
||||||
|
$tech_message = null;
|
||||||
|
if ($ch !== null) {
|
||||||
|
$parts = [];
|
||||||
|
$errno = curl_errno($ch);
|
||||||
|
if ($errno != 0) $parts[] = "errno: $errno";
|
||||||
|
$error = curl_error($ch);
|
||||||
|
if ($error != "") $parts[] = "error: $error";
|
||||||
|
if ($parts) $tech_message = implode(", ", $parts);
|
||||||
|
}
|
||||||
|
parent::__construct($user_message, $tech_message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
namespace nulib\web\ext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class curl: wrapper pour curl
|
||||||
|
*
|
||||||
|
* nécessite l'extension ext-curl
|
||||||
|
*/
|
||||||
|
class curl {
|
||||||
|
private static function _generic(string $url, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
$curlOptions[CURLOPT_URL] = $url;
|
||||||
|
if (!isset($curlOptions[CURLOPT_RETURNTRANSFER])) $curlOptions[CURLOPT_RETURNTRANSFER] = true;
|
||||||
|
$extractHeaders = isset($curlOptions[CURLOPT_HEADER]) && $curlOptions[CURLOPT_HEADER];
|
||||||
|
$ch = curl_init();
|
||||||
|
if ($ch === false) throw new CurlException(null, "init");
|
||||||
|
curl_setopt_array($ch, $curlOptions);
|
||||||
|
try {
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
if ($result === false) throw new CurlException($ch);
|
||||||
|
if ($extractHeaders) {
|
||||||
|
$info = curl_getinfo($ch);
|
||||||
|
$headersSize = $info["header_size"];
|
||||||
|
$headers = substr($result, 0, $headersSize);
|
||||||
|
$headers = explode("\r\n", $headers);
|
||||||
|
$result = substr($result, $headersSize);
|
||||||
|
}
|
||||||
|
return strval($result);
|
||||||
|
} finally {
|
||||||
|
curl_close($ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final function get(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
if ($params !== null) {
|
||||||
|
$query = http_build_query($params);
|
||||||
|
if (strpos($url, "?") !== false) $url = "$url&$query";
|
||||||
|
else $url = "$url?$query";
|
||||||
|
}
|
||||||
|
return self::_generic($url, $curlOptions, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function _custom(string $method, string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
|
||||||
|
if ($params !== null) $curlOptions[CURLOPT_POSTFIELDS] = http_build_query($params);
|
||||||
|
return self::_generic($url, $curlOptions, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
static final function post(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
return self::_custom("POST", $url, $params, $curlOptions, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
static final function put(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
return self::_custom("PUT", $url, $params, $curlOptions, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
static final function delete(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
|
||||||
|
return self::_custom("DELETE", $url, $params, $curlOptions, $headers);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue