Created
February 11, 2024 10:25
-
-
Save dyazincahya/17e6de230b56026f690450eaaa2a73eb to your computer and use it in GitHub Desktop.
Simple Helper file_get_contents PHP for access API with method GET POST PUT and DELETE
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 | |
function makeApiRequest($url, $params = [], $method = 'GET') { | |
$options = [ | |
'http' => [ | |
'method' => $method, | |
'header' => 'Content-Type: application/x-www-form-urlencoded', | |
'content' => http_build_query($params), | |
], | |
]; | |
// If it's a GET request, append parameters to the URL | |
if ($method === 'GET' && !empty($params)) { | |
$url .= '?' . http_build_query($params); | |
} | |
// Create a stream context with the specified options | |
$context = stream_context_create($options); | |
// Make the request using file_get_contents with the created context | |
$response = file_get_contents($url, false, $context); | |
// Return the API response | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to usage?