Created
July 26, 2019 12:21
-
-
Save caionorder/7e90e8e8bcb2a3d7b5595598bdbc8517 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
<?php | |
function callAPI($method, $url, $data, $headers = false){ | |
$curl = curl_init(); | |
switch ($method){ | |
case "POST": | |
curl_setopt($curl, CURLOPT_POST, 1); | |
if ($data) | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
break; | |
case "PUT": | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
if ($data) | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
break; | |
default: | |
if ($data) | |
$url = sprintf("%s?%s", $url, http_build_query($data)); | |
} | |
// OPTIONS: | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'APIKEY: 111111111111111111111', | |
'Content-Type: application/json', | |
)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
// EXECUTE: | |
$result = curl_exec($curl); | |
if(!$result){die("Connection Failure");} | |
curl_close($curl); | |
return $result; | |
} |
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
<?php | |
// GET | |
$get_data = callAPI('GET', 'https://api.example.com/get_url/'.$user['User']['customer_id'], false); | |
$response = json_decode($get_data, true); | |
$errors = $response['response']['errors']; | |
$data = $response['response']['data'][0]; | |
// POST | |
$data_array = array( | |
"customer" => $user['User']['customer_id'], | |
"payment" => array( | |
"number" => $this->request->data['account'], | |
"routing" => $this->request->data['routing'], | |
"method" => $this->request->data['method'] | |
), | |
); | |
$make_call = callAPI('POST', 'https://api.example.com/post_url/', json_encode($data_array)); | |
$response = json_decode($make_call, true); | |
$errors = $response['response']['errors']; | |
$data = $response['response']['data'][0]; | |
// PUT | |
$data_array = array( | |
"amount" => (string)($lease['amount'] / $tenant_count) | |
); | |
$update_plan = callAPI('PUT', 'https://api.example.com/put_url/'.$lease['plan_id'], json_encode($data_array)); | |
$response = json_decode($update_plan, true); | |
$errors = $response['response']['errors']; | |
$data = $response['response']['data'][0]; | |
// DELETE | |
callAPI('DELETE', 'https://api.example.com/delete_url/' . $id, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment