Last active
May 18, 2022 13:12
-
-
Save dzentota/e3df008c899b164de4c89f1878e664b3 to your computer and use it in GitHub Desktop.
Curl vs ExternalResourceClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient; | |
use Sugarcrm\Sugarcrm\Security\HttpClient\RequestException; | |
require 'vendor/autoload.php'; | |
$ch = curl_init(); | |
$url = 'https://httpbin.org/get'; | |
echo 'cURL GET', PHP_EOL, PHP_EOL; | |
// cURL GET | |
curl_setopt_array($ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 60, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS, | |
)); | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$errorNo = curl_errno($ch); | |
$error = curl_error($ch); | |
if ($errorNo !== 0) { | |
$GLOBALS['log']->log("fatal", "curl error ($errorNo): $error"); | |
throw new \SugarApiExceptionError("curl error ($errorNo): $error"); | |
} | |
curl_close($ch); | |
if ($httpCode === 0 || $httpCode >= 400) { | |
$GLOBALS['log']->log("fatal", "Error message: $error"); | |
throw new \SugarApiException($error, null, null, $httpCode ? $httpCode : 500); | |
} | |
echo $response; | |
echo 'ExternalResourceClient GET', PHP_EOL, PHP_EOL; | |
// ExternalResourceClient GET | |
try { | |
$response = (new ExternalResourceClient())->get($url); | |
} catch (RequestException $e) { | |
$GLOBALS['log']->log('fatal', 'Error: ' . $e->getMessage()); | |
throw new \SugarApiExceptionError($e->getMessage()); | |
} | |
$httpCode = $response->getStatusCode(); | |
if ($httpCode >= 400) { | |
$GLOBALS['log']->log("fatal", "Request failed with status: " . $httpCode); | |
throw new \SugarApiException("Request failed with status: " . $httpCode, null, null, $httpCode); | |
} | |
echo $response->getBody()->getContents(); | |
// cURL POST JSON | |
$url = 'https://httpbin.org/post'; | |
echo 'cURL POST JSON', PHP_EOL, PHP_EOL; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("Content-Type: application/json")); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['foo' => 'bar'])); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$parsed = !empty($response) ? json_decode($response, true) : null; | |
var_dump($parsed); | |
// ExternalResourceClient POST JSON | |
echo 'cURL ExternalResourceClient JSON', PHP_EOL, PHP_EOL; | |
try { | |
$response = (new ExternalResourceClient())->post($url, json_encode(['foo' => 'bar']), ['Content-Type' => "application/json"]); | |
} catch (RequestException $e) { | |
throw new \SugarApiExceptionError($e->getMessage()); | |
} | |
$parsed = !empty($response) ? json_decode($response->getBody()->getContents(), true) : null; | |
var_dump($parsed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment