i'm trying using cURL for a GET request like this:
function connect($id_user){ $ch = curl_init(); $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); $body = '{}'; curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_POSTFIELDS,$body); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 30); $authToken = curl_exec($ch); return $authToken; }As you an see i want to pass $body as the request's body , but i don't know if its correct or not and i can't debug this actually, do you know if is the right to use curl_setopt($ch, CURLOPT_POSTFIELDS,$body); with a GET request?
Cause this enteire code works perfect with POST, now i'm trying change this to GET as you can see
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, '//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.
cURL is software which you can use to make various requests using different protocols. PHP has the option to use cURL and in this article, we’ll show several examples.
PHP cURL Basics
curl_init(); // initializes a cURL session curl_setopt(); // changes the cURL session behavior with options curl_exec(); // executes the started cURL session curl_close(); // closes the cURL session and deletes the variable made by curl_init();PHP cURL POST Request
A POST request is usually made to send user collected data to a server.
PHP cURL GET Request
A GET request retrieves data from a server. This can be a website’s HTML, an API response or other resources.
<?php $cURLConnection = curl_init(); curl_setopt($cURLConnection, CURLOPT_URL, '//hostname.tld/phone-list'); curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true); $phoneList = curl_exec($cURLConnection); curl_close($cURLConnection); $jsonArrayResponse - json_decode($phoneList);PHP cURL Header
You can also set custom headers in your cURL requests. For this, we’ll use the curl_setopt() function.
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Header-Key: Header-Value', 'Header-Key-2: Header-Value-2' ));Check other PHP related commands and common how to`s. Get a new shared hosting account with up to 40% discount and master your PHP skills.
I want to use cURL for a GET request like this:
function connect($id_user){ $ch = curl_init(); $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); $body = '{}'; curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_POSTFIELDS,$body); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 30); $authToken = curl_exec($ch); return $authToken; }Do you know if it is correct to use curl setopt($ch, CURLOPT POSTFIELDS,$body); with a GET request? As you can see, I want to give $body as the request's body. However, I'm not sure if this is correct or not, and I'm actually unable to debug this.