Click here to Skip to main content
12,252,903 members (60,115 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.8K views
Posted

POST JSON Data With PHP cURL Wrapper Class

, 3 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
POST JSON data with PHP cURL wrapper class

PHP biceps cURL :)

Following is the PHP cURL wrapper class which I use to make GET and POST requests. The examples are below. Disclaimer: Be sure that you have permission to scrape and use the content you’re after.

This code is also available on GitHub at this link: https://github.com/Hitman666/PHPcURLWrapper

Class Code and Simple GET Request

//CurlWrapper_static.php

class CurlWrapper {
    private static $useragents = array(            
        "Mozilla/5.0 (Windows NT 6.1; WOW64) 
        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident/4.0; SLCC1)",
        "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) 
        AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
    );

    private static $cookiesFile = "curlCookies.txt";

    private static function getUserAgent() {
        $rand = rand(0, count(self::$useragents) - 1);

        return self::$useragents[$rand];
    }

    public static function SendRequest($url, $ref = "", 
    $type = "GET", $postData = "", $headers = "", $proxy = "") {
        $useragent = self::getUserAgent();

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_TIMEOUT,120);
        curl_setopt($ch, CURLOPT_USERAGENT, self::getUserAgent());

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);

        curl_setopt($ch, CURLOPT_COOKIEJAR, realpath(self::$cookiesFile)); 
        curl_setopt($ch, CURLOPT_COOKIEFILE, realpath(self::$cookiesFile));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        //options
        if ($ref != "") {
            curl_setopt($ch, CURLOPT_REFERER, $ref);
        }

        if ($proxy != "") {
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
        }

        if ($type == "POST"){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }

        if ($headers != ""){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
}

Simple GET request:

require("CurlWrapper_static.php");

$googleHTML = CurlWrapper::SendRequest('https://www.google.com');
echo $googleHTML;

If you’re a firm non-static lover, here’s a “normal” class:

//CurlWrapper_nonStatic.php

class CurlWrapper{    
    private $_useragents = array(            
        "Mozilla/5.0 (Windows NT 6.1; WOW64) 
        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident/4.0; SLCC1)",
        "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) 
        AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
    );

    private $_cookiesFile = "curlCookies.txt";

    private function getUserAgent(){
        $rand = rand(0, count($this->_useragents));

        return $useragents[$rand];
    }

    public function SendRequest($url, $ref = "", $type = "GET", 
    $postData = "", $headers = "", $proxy = "") {
        $useragent = $this->getUserAgent();
        echo $useragent;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_TIMEOUT,120);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);

        curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($this->_cookiesFile)); 
        curl_setopt($ch, CURLOPT_COOKIEFILE, realpath($this->_cookiesFile));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        //options
        if ($ref != "") {
            curl_setopt($ch, CURLOPT_REFERER, $ref);
        }

        if ($proxy != "") {
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
        }

        if ($type == "POST"){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }

        if ($headers != ""){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
} 

How to use it with simple GET request:

require("CurlWrapper_nonStatic.php");

$curl = new CurlWrapper();
$googleHTML = $curl->SendRequest('https://www.google.com');
echo $googleHTML;

JSON POST Request

Here’s an example of sending a JSON POST request to an imaginative URL ‘http://service.com/getData.json’ with some data array:

require("CurlWrapper_static.php");

    $cookieSettingUrl = 'http://service.com/';
    $cookie = CurlWrapper::SendRequest($cookieSettingUrl);

    $data = array(
        "year" => 2014,
        "day" => 3,
                "month" => 4,
        "id" => 20
    );
    $postData = json_encode($data);

    $jsonUrl = 'http://service.com/getData.json';
    $headers = array('Accept: application/json','Content-Type: application/json');

    $resultsHTML = CurlWrapper::SendRequest($jsonUrl, $cookieSettingUrl, 
    "POST", $postData, $headers);
    $resultsJson = json_decode($resultsHTML);
    var_dump($resultsJson);

It is important to note that you have to add proper $headers array, and that you json_encode your data array as shown when POSTing to a service which expects JSON data.

Cookies

The reason why I first used these two lines:

$cookieSettingUrl = 'http://service.com/';
$cookie = CurlWrapper::SendRequest($cookieSettingUrl);

is to set any cookies (and you will find that some services do this) that may be needed to be sent along with the request to ‘http://service.com/getData.json’. Cookies are set to ‘curlCookies.txt’ in the CurlWrapper_* class. You can change this to your liking, and you have to make sure that you set proper permissions for this file.

Hope this proves useful to someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nikola Breznjak
Web Developer
Croatia Croatia
Nikola Brežnjak is an engineer at heart and a jack of all trades kind of guy.

For those who care about titles, he has a masters degree in computing from FER. For the past 7 years he worked in a betting software industry where he made use of his knowledge in areas ranging from full stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).

Lately he’s been interested in the MEAN stack, Ionic framework and Unity3D. Also, he likes to help out on StackOverflow where he’s in the top 0.X% currently.

Come and see what I write about on my blog.

My book on MEAN stack.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 3 Apr 2014
Article Copyright 2014 by Nikola Breznjak
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid