djekl
6/18/2014 - 1:25 PM

Example Curl Class Wrapper

Example Curl Class Wrapper

<?php

class CurlRequest
{
    private $api_key;
    private $ch;

    public function __construct($api_key)
    {
        $this->api_key = $api_key;
    }

    /**
     * Init curl session
     *
     * $params = array(
     *     'url' => '',
     *     'host' => '',
     *     'header' => '',
     *     'method' => '',
     *     'referer' => '',
     *     'cookie' => '',
     *     'post_fields' => array(),
     *     'login' => '',
     *     'password' => '',
     *     'timeout' => 0,
     * );
     */
    public function init($params = array())
    {
        $params = array_merge(
            array(
                'url'         => '',
                'host'        => '',
                'header'      => '',
                'method'      => 'get',
                'referer'     => '',
                'cookie'      => '',
                'post_fields' => array(),
                'timeout'     => 20,
            ),
            $params
        );

        $this->ch = curl_init();

        $user_agent = 'curl';

        $header = array(
            'X_Auth' => $this->api_key,
            'Accept-Language' => 'en-GB',
        );

        if (!empty($params['host'])) {
            $header['Host'] = $host;
        }

        if (!empty($params['header'])) {
            $header += $params['header'];
        }

        $header = http_build_query($header);

        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_VERBOSE, true);
        curl_setopt($this->ch, CURLOPT_HEADER, true);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt($this->ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($this->ch, CURLOPT_URL, $params['url']);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $params['timeout']);

        if (strtolower($params['method']) === 'head') {
            curl_setopt($this->ch, CURLOPT_NOBODY, true);
        }

        if (!empty($params['referer'])) {
            curl_setopt($this->ch, CURLOPT_REFERER, $params['referer'] );
        }

        if (!empty($params['cookie'])) {
            curl_setopt($this->ch, CURLOPT_COOKIE, $params['cookie']);
        }

        if ( $params['method'] == "POST" ) {
            curl_setopt($this->ch, CURLOPT_POST, true );
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params['post_fields']);
        }

        if (!empty($params['login']) && !empty($params['password'])) {
            curl_setopt($this->ch, CURLOPT_USERPWD, $params['login'] . ':' . $params['password']);
        }
    }

    /**
     * Make curl request
     *
     * return array  'header','body','curl_error','http_code','last_url'
     */
    public function exec()
    {
        $response = curl_exec($this->ch);
        $error = curl_error($this->ch);

        $result = array(
            'header'     => '',
            'body'       => '',
            'curl_error' => '',
            'http_code'  => '',
            'last_url'   => '',
        );

        if (!empty($error)) {
            $result['curl_error'] = $error;

            return $result;
        }

        $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);

        $result['header']    = substr($response, 0, $header_size);
        $result['body']      = substr($response, $header_size);
        $result['http_code'] = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
        $result['last_url']  = curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL);

        return $result;
    }
}


// Example of use:
$api_key = '';
$curl = new CurlRequest($api_key);

$curl->init(array(
    'url'         => 'https://xboxapi.com/v2/accountXuid',
    'host'        => '',
    'header'      => '',
    'method'      => 'GET', // 'POST','HEAD'
    'referer'     => '',
    'cookie'      => '',
    'post_fields' => array(),
    'timeout'     => 20,
));

try {
    $result = $curl->exec();

    if ($result['curl_error']) {
        throw new Exception($result['curl_error']);
    }

    if (substr($result['http_code'], 0, 1) != 2) {
        throw new Exception('HTTP Code = ' . $result['http_code']);
    }

    if (empty($result['body'])) {
        throw new Exception('Body of file is empty');
    }
} catch (Exception $e) {
    echo $e->getMessage();
}