Created
April 6, 2020 19:13
-
-
Save williankeller/43dc53f3edc774710f1602a21c7e4d44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * @param $url | |
| * @param null $requestBody | |
| * @return array | |
| * @throws LocalizedException | |
| */ | |
| public function createRequest($url, $requestBody = null): array | |
| { | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, $url); | |
| curl_setopt($curl, CURLOPT_ENCODING, ""); | |
| curl_setopt($curl, CURLOPT_MAXREDIRS, 10); | |
| curl_setopt($curl, CURLOPT_TIMEOUT, 0); | |
| curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
| curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
| curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody); | |
| $headers = [ | |
| 'Content-Type: application/json' | |
| ]; | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| $response = curl_exec($curl); | |
| $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| $error_code = curl_errno($curl); | |
| $error = curl_error($curl); | |
| if ($error_code == 28 && $httpStatus == 0) { | |
| throw new LocalizedException(__('Error')); | |
| } | |
| curl_close($curl); | |
| if ($error_code) { | |
| throw new LocalizedException(__($error . $error_code)); | |
| } | |
| return ['status' => $httpStatus, 'body' => $response]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment