src/HttpClient/CurlClient.php
public function request($method, $absUrl, $params, $headers)
    {
        $curl = curl_init();
        $method = strtolower($method);

        $opts = array();

        if ($method == 'get') {
            $opts[CURLOPT_HTTPGET] = 1;
            if (count($params) > 0) {
                $encoded = self::urlEncode($params);
                $absUrl = "$absUrl?$encoded";
            }
        } elseif ($method == 'post') {
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = json_encode($params);
        } elseif ($method == 'delete') {
            $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
            if (count($params) > 0) {
                $encoded = self::urlEncode($params);
                $absUrl = "$absUrl?$encoded";
            }
        } else {
            throw new Error("Unrecognized method $method");
        }

        // Create a callback to capture HTTP headers for the response
        $rheaders = array();
        $headerCallback = function ($curl, $header_line) use (&$rheaders) {
            // Ignore the HTTP request line (HTTP/1.1 200 OK)
            if (strpos($header_line, ":") === false) {
                return strlen($header_line);
            }
            list($key, $value) = explode(":", trim($header_line), 2);
            $rheaders[trim($key)] = trim($value);
            return strlen($header_line);
        };

        // By default for large request body sizes (> 1024 bytes), cURL will
        // send a request without a body and with a `Expect: 100-continue`
        // header, which gives the server a chance to respond with an error
        // status code in cases where one can be determined right away (say
        // on an authentication problem for example), and saves the "large"
        // request body from being ever sent.
        //
        // Unfortunately, the bindings don't currently correctly handle the
        // success case (in which the server sends back a 100 CONTINUE), so
        // we'll error under that condition. To compensate for that problem
        // for the time being, override cURL's behavior by simply always
        // sending an empty `Expect:` header.
        array_push($headers, 'Expect: ');

        $opts[CURLOPT_URL] = $absUrl;
        $opts[CURLOPT_RETURNTRANSFER] = true;
        $opts[CURLOPT_CONNECTTIMEOUT] = 80;
        $opts[CURLOPT_TIMEOUT] = 30;
        $opts[CURLOPT_HEADERFUNCTION] = $headerCallback;
        $opts[CURLOPT_HTTPHEADER] = $headers;

        curl_setopt_array($curl, $opts);
        $rbody = curl_exec($curl);

        if ($rbody === false) {
            $errno = curl_errno($curl);
            $message = curl_error($curl);
            curl_close($curl);
            $this->handleCurlError($errno, $message);
        }

        $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        return array(json_decode($rbody), $rcode, $rheaders);
    }
public function request($method, $absUrl, $params, $headers)
    {
        $curl = curl_init();
        $method = strtolower($method);

        $opts = array();

        if ($method == 'get') {
            $opts[CURLOPT_HTTPGET] = 1;
            if (count($params) > 0) {
                $encoded = self::urlEncode($params);
                $absUrl = "$absUrl?$encoded";
            }
        } elseif ($method == 'post') {
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = json_encode($params);
        } elseif ($method == 'delete') {
            $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
            if (count($params) > 0) {
                $encoded = self::urlEncode($params);
                $absUrl = "$absUrl?$encoded";
            }
        } else {
            throw new Error("Unrecognized method $method");
        }

        // Create a callback to capture HTTP headers for the response
        $rheaders = array();
        $headerCallback = function ($curl, $header_line) use (&$rheaders) {
            // Ignore the HTTP request line (HTTP/1.1 200 OK)
            if (strpos($header_line, ":") === false) {
                return strlen($header_line);
            }
            list($key, $value) = explode(":", trim($header_line), 2);
            $rheaders[trim($key)] = trim($value);
            return strlen($header_line);
        };

        // By default for large request body sizes (> 1024 bytes), cURL will
        // send a request without a body and with a `Expect: 100-continue`
        // header, which gives the server a chance to respond with an error
        // status code in cases where one can be determined right away (say
        // on an authentication problem for example), and saves the "large"
        // request body from being ever sent.
        //
        // Unfortunately, the bindings don't currently correctly handle the
        // success case (in which the server sends back a 100 CONTINUE), so
        // we'll error under that condition. To compensate for that problem
        // for the time being, override cURL's behavior by simply always
        // sending an empty `Expect:` header.
        array_push($headers, 'Expect: ');

        $opts[CURLOPT_URL] = $absUrl;
        $opts[CURLOPT_RETURNTRANSFER] = true;
        $opts[CURLOPT_CONNECTTIMEOUT] = 80;
        $opts[CURLOPT_TIMEOUT] = 30;
        $opts[CURLOPT_HEADERFUNCTION] = $headerCallback;
        $opts[CURLOPT_HTTPHEADER] = $headers;

        curl_setopt_array($curl, $opts);
        $rbody = curl_exec($curl);

        if ($rbody === false) {
            $errno = curl_errno($curl);
            $message = curl_error($curl);
            curl_close($curl);
            $this->handleCurlError($errno, $message);
        }

        $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        return array(json_decode($rbody), $rcode, $rheaders);
    }
public function request ( $ method , $ absUrl , $ params , $ headers ) { $ curl = curl_init ( ) ; $ method = strtolower ( $ method ) ; $ opts = array ( ) ; if ( $ method == 'get' ) { $ opts [ CURLOPT_HTTPGET ] = 1 ; if ( count ( $ params ) > 0 ) { $ encoded = self :: urlEncode ( $ params ) ; $ absUrl = "$absUrl?$encoded" ; } } elseif ( $ method == 'post' ) { $ opts [ CURLOPT_POST ] = 1 ; $ opts [ CURLOPT_POSTFIELDS ] = json_encode ( $ params ) ; } elseif ( $ method == 'delete' ) { $ opts [ CURLOPT_CUSTOMREQUEST ] = 'DELETE' ; if ( count ( $ params ) > 0 ) { $ encoded = self :: urlEncode ( $ params ) ; $ absUrl = "$absUrl?$encoded" ; } } else { throw new Error ( "Unrecognized method $method" ) ; } // Create a callback to capture HTTP headers for the response $ rheaders = array ( ) ; $ headerCallback = function ( $ curl , $ header_line ) use ( & $ rheaders ) { // Ignore the HTTP request line (HTTP/1.1 200 OK) if ( strpos ( $ header_line , ":" ) === false ) { return strlen ( $ header_line ) ; } list ( $ key , $ value ) = explode ( ":" , trim ( $ header_line ) , 2 ) ; $ rheaders [ trim ( $ key ) ] = trim ( $ value ) ; return strlen ( $ header_line ) ; } ; // By default for large request body sizes (> 1024 bytes), cURL will // send a request without a body and with a `Expect: 100-continue` // header, which gives the server a chance to respond with an error // status code in cases where one can be determined right away (say // on an authentication problem for example), and saves the "large" // request body from being ever sent. // // Unfortunately, the bindings don't currently correctly handle the // success case (in which the server sends back a 100 CONTINUE), so // we'll error under that condition. To compensate for that problem // for the time being, override cURL's behavior by simply always // sending an empty `Expect:` header. array_push ( $ headers , 'Expect: ' ) ; $ opts [ CURLOPT_URL ] = $ absUrl ; $ opts [ CURLOPT_RETURNTRANSFER ] = true ; $ opts [ CURLOPT_CONNECTTIMEOUT ] = 80 ; $ opts [ CURLOPT_TIMEOUT ] = 30 ; $ opts [ CURLOPT_HEADERFUNCTION ] = $ headerCallback ; $ opts [ CURLOPT_HTTPHEADER ] = $ headers ; curl_setopt_array ( $ curl , $ opts ) ; $ rbody = curl_exec ( $ curl ) ; if ( $ rbody === false ) { $ errno = curl_errno ( $ curl ) ; $ message = curl_error ( $ curl ) ; curl_close ( $ curl ) ; $ this -> handleCurlError ( $ errno , $ message ) ; } $ rcode = curl_getinfo ( $ curl , CURLINFO_HTTP_CODE ) ; curl_close ( $ curl ) ; return array ( json_decode ( $ rbody ) , $ rcode , $ rheaders ) ; }
Make the request

@param string $method
@param string $absUrl
@param array $params
@param array $headers

@return array A zero indexed array.
0 => Response
1 => Response http code
2 => Array of response headers
Make the request