Friday 15 July 2011

PHP cURL GET request and request's body -


I'm trying to use cURL for a GET request like this:

 < Code> function connected ($ Id_user) {$ ch = curl_init (); $ Header = array ('approve: app / jason', 'content-type: app / json'); Curl_setopt ($ ch, CURLOPT_URL, $ this- & gt; 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); // timed second curl_setopt ($ ch, CURLOPT_TIMEOUT, 30); $ AuthToken = curl_exec ($ ch); Return $ authToken; }   

As you see, I want to pass $ body as the request body, but I do not know whether it is correct and I actually debug it Can not you know, if you have the right to use curl_setopt ($ CH, CURLOPT_POSTFIELDS, $ body); with a GET request?

Because this enteire code works perfectly with POST, now I am trying to change it to change it as you can see

< Div class = "post-text" itemprop = "text">

CURLOPT_POSTFIELDS as the name suggests, for a post request body (payload) For the received requests, the payload is part of the URL as a query string.

In your case, you need to create a URL that is required to send you (if any), and remove other options in the CRL.

  curl_setopt ($ ch, CURLOPT_URL, $ this-> Services_urzell.username '. $ 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);    

No comments:

Post a Comment