Created
March 10, 2023 20:16
-
-
Save ederrafo/f5cd802f1995c3a13f010e648a7dcb0f to your computer and use it in GitHub Desktop.
curl php
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 | |
private function restTemplate($method, $uriTemplate, $request, $logging = []) | |
{ | |
$account_id = null; | |
if (isset($request['accountId'])) { | |
$account_id = $request['accountId']; | |
} | |
$pnr = null; | |
if (isset($request['reference'])) { | |
$pnr = $request['reference']; | |
} | |
$curl_init = curl_init(); | |
$uri = $this->url . '/' . $uriTemplate; | |
$uri = str_replace(" ", "%20", $uri); | |
$json_encode = json_encode($request); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__ , 1, " >>> Request Method: " . $method); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__ , 1, " >>> Request URI: " . $uri); | |
curl_setopt($curl_init, CURLOPT_HEADER, 1); | |
curl_setopt($curl_init, CURLOPT_URL, $uri); | |
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true); | |
if (in_array($method, array('POST', 'PATCH'))) { | |
if ($method == "POST") { | |
curl_setopt($curl_init, CURLOPT_POST, 1); | |
} elseif ($method == "PATCH") { | |
curl_setopt($curl_init, CURLOPT_CUSTOMREQUEST, 'PATCH'); | |
} | |
curl_setopt($curl_init, CURLOPT_POSTFIELDS, $json_encode); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__ , 1, " >>> Request Body: " . $json_encode); | |
curl_setopt($curl_init, CURLOPT_HTTPHEADER, | |
array( | |
'Content-Type: application/json' | |
) | |
); | |
} | |
$message = curl_exec($curl_init); | |
$status_code = curl_getinfo($curl_init,CURLINFO_HTTP_CODE); | |
$header_size = curl_getinfo($curl_init, CURLINFO_HEADER_SIZE); | |
$header = substr($message, 0, $header_size); | |
$body = substr($message, $header_size); | |
$result = true; | |
if (curl_errno($curl_init)) { | |
$result = false; | |
$message = curl_error($curl_init); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__ , 2, $message); | |
} | |
curl_close($curl_init); | |
if ($status_code == 200) { | |
$body = json_decode($body, true); | |
} | |
$response = array( | |
'status_code' => $status_code, | |
'header_size' => $header_size, | |
'header' => $header, | |
'result' => $result, | |
'body' => $body, | |
'message' => $message, | |
'uri' => $uri, | |
); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__, 1, " <<< Response Code Status : " . $status_code); | |
$this->logger->save(basename(__FILE__) . " in " . __FUNCTION__ . " on line ". __LINE__, 1, " <<< Response: " . json_encode($body)); | |
$this->log($uri, $uriTemplate . ' - ' . $method, $request, $body, $logging); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment