Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL GET request and request's body

Tags:

php

curl

http-get

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

like image 233
itsme Avatar asked Jun 21 '13 07:06

itsme


People also ask

How does PHP handle curl request?

php file with the following contents. $url = 'https://www.example.com' ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url );

How do you get the curl Body request?

To post data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The Content-Type header indicates the data type in the body of the request message.

What is Curl_exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


2 Answers

The accepted answer is wrong. GET requests can indeed contain a body. This is the solution implemented by WordPress, as an example:

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); 

EDIT: To clarify, the initial curl_setopt is necessary in this instance, because libcurl will default the HTTP method to POST when using CURLOPT_POSTFIELDS (see documentation).

like image 89
Dan Avatar answered Oct 03 '22 11:10

Dan


CURLOPT_POSTFIELDS as the name suggests, is for the body (payload) of a POST request. For GET requests, the payload is part of the URL in the form of a query string.

In your case, you need to construct the URL with the arguments you need to send (if any), and remove the other options to cURL.

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); 
like image 36
Burhan Khalid Avatar answered Oct 03 '22 12:10

Burhan Khalid