Sending a GET request with a request body with PHP cURL
Published: April 16, 2020
Some APIs require GET requests with request bodies. I was looking into how to do that today and struggling with Google. Eventually I found this answer on StackOverflow.
PHP code is as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maxchadwick.xyz');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'THIS IS THE REQUEST BODY');
curl_exec($ch);
Some of the other answers on the StackOverflow thread are incorrect…If you remove CURLOPT_CUSTOMREQUEST
it will NOT default to a GET
if you use CURLOPT_POSTFIELDS
but will instead be a POST
.
I used Burp Suite to intercept the request and confirm the above was doing a GET with a request body.