Detecting request type in PHP (GET, POST, PUT or DELETE)
By using
$_SERVER['REQUEST_METHOD']
Example
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// The request is using the POST method
}
For more details please see the documentation for the $_SERVER variable.
PHP detecting request type (GET, POST, PUT or DELETE) without using $_SERVER['REQUEST_METHOD']
Regular if statements
if(!empty($_GET)) {
$request = (!empty($_POST)) ? 'both get and post' : 'get';
} else if(!empty($_POST)) {
$request = 'post';
}
//... You get the picture
Edit: I added a ternary within the get check to solve a problem that Gumbo noted in the comments. You can have both GET and POST vars available as you can POST data to a url with get params, i.e. /forms/addFileToCompany/?companyId=23
And now because I am a complete filth, the most horrible ternary you have ever seen! Note this is just for a bit of fun and I really do not recommend using it.
$request = (!empty($_GET))
? (!empty($_POST))
? 'both post and get'
: 'get'
: (!empty($_POST))
? 'post'
: (/* Keep it going for whatever */ );
Check whether a request is GET or POST
Better use $_SERVER['REQUEST_METHOD']
:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
POST and GET works in Postmen but PUT and DELETE doesn't
In the case of PUT and DELETE you need to get the request parameters this way:
$requestParams = array();
parse_str(file_get_contents('php://input'), $requestParams);
Then you can access the values like using $_GET or $_POST:
$requestParams['product_id']
Hope this helps.
Laravel POST api request going as null. how to make post request in laravel
You can try the following code
$uri = "https://example.com/api/your-api-url-here";
$params['headers'] = [
'Content-type' => 'application/json'
];
$params['body'] = [
'username' => 'helloWorld'
];
$client = new Client();
$response = $client->request('POST', $uri, $params);
$data = json_decode($response->getBody(), true);
dd($data);
And don't forget to use Guzzle/Http in your project, you simply can install guzzle http and use it in your controller like
use GuzzleHttp\Client;
How to start a GET/POST/PUT/DELETE request and judge request type in PHP?
For DELETE
use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
For PUT
use curl_setopt($ch, CURLOPT_PUT, true);
An alternative that doesn't rely on cURL being installed would be to use file_get_contents
with a custom HTTP stream context.
$result = file_get_contents(
'http://example.com/submit.php',
false,
stream_context_create(array(
'http' => array(
'method' => 'DELETE'
)
))
);
Check out these two articles on doing REST with PHP
- http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
- http://www.gen-x-design.com/archives/making-restful-requests-in-php/
Related Topics
MySQL and PHP: Utf-8 With Cyrillic Characters
PHP: How to Send Http Response Code
How to Close a Connection Early
What Are the PHP Operators "" and ":" Called and What Do They Do
Multiple Returns from a Function
What Does the Variable $This Mean in PHP
Finding Cartesian Product With PHP Associative Arrays
How to Extract Data from CSV File in PHP
Creating One Array from Another Array in PHP
How to Convert an Image to Base64 Encoding
How to Upload File Using Curl With PHP
How to Add Elements to an Empty Array in PHP
Htaccess: Add/Remove Trailing Slash from Url
How to Implement a Good Profanity Filter