- Added a process to native handler
This commit is contained in:
parent
d94ee83230
commit
3590993925
|
@ -9,4 +9,21 @@ class NativeHandler extends Handler {
|
|||
return $this->exceptionHandler->extractExceptionData($ex, $_SERVER, $_POST);
|
||||
}
|
||||
|
||||
public function process(callable $callback) : mixed
|
||||
{
|
||||
try {
|
||||
return $callback();
|
||||
}
|
||||
catch (\Throwable $ex)
|
||||
{
|
||||
$this->pushData($ex);
|
||||
|
||||
if ( $this->callback ?? false ) {
|
||||
return call_user_func_array($this->callback, [ $ex ] );
|
||||
}
|
||||
else {
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,11 @@ namespace Negundo\Client\Transport;
|
|||
|
||||
class Curl implements TransportInterface {
|
||||
|
||||
public $timeout = 1;
|
||||
public $timeout = 2;
|
||||
|
||||
public $throwErrors = false;
|
||||
public $throwErrors = true;
|
||||
|
||||
public $verifySsl = false;
|
||||
|
||||
public $headers = [];
|
||||
|
||||
|
@ -20,15 +22,15 @@ class Curl implements TransportInterface {
|
|||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl);
|
||||
|
||||
$exec = curl_exec($ch);
|
||||
|
||||
if ( ( false === $exec ) && $this->throwErrors ) {
|
||||
$errno = curl_errno($ch);
|
||||
$errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]);
|
||||
curl_close($ch);
|
||||
|
||||
throw new CurlException(implode(PHP_EOL, $errors), $errno);
|
||||
throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno);
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
@ -36,6 +38,30 @@ class Curl implements TransportInterface {
|
|||
return $exec;
|
||||
}
|
||||
|
||||
public function get(string $url) : ? object
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers );
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl);
|
||||
$execute = curl_exec($ch);
|
||||
|
||||
if ( ( false === $execute ) && $this->throwErrors ) {
|
||||
$errno = curl_errno($ch);
|
||||
|
||||
curl_close($ch);
|
||||
throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno);
|
||||
}
|
||||
else {
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
return $execute ? (object) $execute : null;
|
||||
}
|
||||
|
||||
const CURL_ERROR = [
|
||||
0 => 'CURLE_OK',
|
||||
1 => 'CURLE_UNSUPPORTED_PROTOCOL',
|
||||
|
|
Loading…
Reference in New Issue