How do I make get and post requests through Curl?

Asked

Viewed 2,267 times

1

I have this class with the following methods :

 public function __construct($url,$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false) 
 { 
    //setando valores nas variáveis  
     $this->_url = $url; 
     $this->_followlocation = $followlocation; 
     $this->_timeout = $timeOut; 
     $this->_maxRedirects = $maxRedirecs; 
     $this->_noBody = $noBody; 
     $this->_includeHeader = $includeHeader; 
     $this->_binaryTransfer = $binaryTransfer; 
     $this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt'; 
 } 


public function buildCurl($url = 'nul') 
{ 
    if($url != 'nul'){ 
      $this->_url = $url; 
    } 

     $s = curl_init(); 

     curl_setopt($s,CURLOPT_URL,$this->_url); 
     curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:')); 
     curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout); 
     curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects); 
     curl_setopt($s,CURLOPT_RETURNTRANSFER,true); 
     curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation); 
     curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation); 
     curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation); 

     if($this->authentication == 1){ 
       curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass); 
     } 
     if($this->_post) 
     { 
         curl_setopt($s,CURLOPT_POST,true); 
         curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields); 

     } 

     if($this->_includeHeader) 
     { 
           curl_setopt($s,CURLOPT_HEADER,true); 
     } 

     if($this->_noBody) 
     { 
         curl_setopt($s,CURLOPT_NOBODY,true); 
     } 
     /* 
     if($this->_binary) 
     { 
         curl_setopt($s,CURLOPT_BINARYTRANSFER,true); 
     } 
     */ 
     curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent); 
     curl_setopt($s,CURLOPT_REFERER,$this->_referer); 

     $this->_webpage = curl_exec($s); 
     $this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE); 

     curl_close($s); 

     } 

     public function getResponse($url)
     { 
       $this->buildCurl($url);

       return $this->_webpage; 
     } 

and here the method call in another file in my controller .

     public function cURL($id)
     {
       $_url = "https://receitacpf.cf/api/cpf/$id";

       $curl = new Curl($_url);

       return $curl->getResponse($_url);
     }

I wonder why is not giving any more error also is not bringing anything , to break the head in this

  • You’re just returning somewhere to sponse, and not printing it. Run a print_r() or var_dump() or var_export() return this function and show us what appears.

  • gave a var_dump() and at least already return something like this : string(3309) " "

  • If you access this API and place your CPF https://receitacpf.cf/api/cpf/ you will see that the total of characters you will get on the page is 3309.

  • Run the test at http://onlinecurl.com/. Enter the api URL + a Cpf.

  • Explaining what’s happening... While executing echo or var_dump() you will see everything blank, however, right-click anywhere on the blank screen and go to Exibir o código fonte da página. Then you will see that the result of your cURL is there.

  • thanks for the help , more as I would have to do to bring for example result otherwise ?

Show 1 more comment

1 answer

3


You can do the following:

$url='https://receitacpf.cf/api/cpf/NUMERODOCPF';

$ch = curl_init($url);

$request_headers = array();
$request_headers[] = 'Accept-Encoding: gzip';
$request_headers[] = 'Client: Apple';

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); /* Moved this line here */

curl_setopt($ch, CURLOPT_COOKIE, 'insertedmycookiehere');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;
  • They fight for help, man

  • @Stone Man You’re welcome :]

  • and in that code if I don’t want to bring the message zipped , if I really want to tazer the message as I would do I omitted this line $request_headers[] = 'Accept-Encoding: gzip'; and gave error

  • You can choose which encoding you want to use. Gives a read at this link!

  • guy hasn’t pulled out yet if I co-co-print > 'Accept-Encoding: gzip'; and replace by nothing or null as it says in the link article of the bug bro

  • and I just wanted to bring the message clean without being encrypted in any way @Godfrey the King

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.