how do I make get and post requests through curl?

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 is the method call to 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 would like to know why you are not making any more mistakes, you are not bringing anything, so breaking your head on it

    
asked by anonymous 25.08.2017 / 16:24

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;
    
25.08.2017 / 17:07