Curl Request POST | GET

public function makePostRequest($url, $data, $timeout = self::HTTP_REQUEST_TIMEOUT, $parse_result = true)
    {   
        $is_json='';   
        $ch = curl_init($url);       
        $content_type = $is_json ? 'application/json' : 'application/x-www-form-urlencoded';
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $parsed_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,$timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.$content_type, 'Content-length: '.Tools::strlen($parsed_data)));                                                                                                                 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close ($ch);   
               
    }

    private function makeGetRequest($url, $data = array(), $timeout = self::HTTP_REQUEST_TIMEOUT)
    {
        if(count($data) > 0) {
            $url .= '?' . http_build_query($data);   
        }
        $ch = curl_init($url);                                                                                                                    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,$timeout);                                                                                                                  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); /* Added by PrestaShop */
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); /* Added by PrestaShop */       
        $result = curl_exec($ch);
        curl_close ($ch);   
        return $result;
    }

Comments

Popular Posts