PHP CURL EXAMPLES – HTTP GET, POST, PUT, RAW

You can use those functions to make http get, post, put and raw requests.

function httpPostRequest($url, $requestType, $postData)

    {

        $ch = curl_init();

 

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_COOKIESESSION, true);

        curl_setopt($ch, CURLOPT_POST, 1);

 

        if ($requestType == "RAW") {

            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

        } else if ($requestType == "JSON") {

            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

        } else {

            curl_setopt($ch, CURLOPT_HEADER, 0);

            curl_setopt($ch, CURLOPT_POSTFIELDS,

                http_build_query($postData)

            );

        }

 

 

        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');

        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');

 

        $result = curl_exec($ch);

 

 

        return $result;

 

        curl_close($ch);

    }

 

 

    function httpGetRequest($url){

 

        $ch = curl_init();

 

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_COOKIESESSION, true);

        curl_setopt($ch, CURLOPT_HEADER, 0);

 

        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');

        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');

 

        $result = curl_exec($ch);

 

 

        return $result;

        curl_close($ch);

    }

Leave a Reply

Your email address will not be published. Required fields are marked *