2017 © Pedro Peláez
 

library curl

A basic CURL wrapper for PHP

image

alioygur/curl

A basic CURL wrapper for PHP

  • Wednesday, November 5, 2014
  • by alioygur
  • Repository
  • 2 Watchers
  • 6 Stars
  • 28 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Curl

A basic CURL wrapper for PHP (see http://php.net/curl for more information about the libcurl extension for PHP), (*1)

Installation

This library is available via Composer, (*2)

{
    "require": {
        "alioygur/curl": "~1.0"
    }
}

Usage

Simple example of usage

Simply initialize and usage the Curl class like so:, (*3)

<?php
use Alioygur\Curl\Curl;

$curl = new Curl();

$response = $curl
    ->setOption('CURLOPT_FOLLOW_REDIRECTS', false)
    ->setHeader('User-Agent', 'My Name is Heisenberg!')
    ->get('http://example.com');

Performing a Request

The Curl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify a url to request and optionally specify an associative array or string of variables to send along with it., (*4)

$response = $curl->head($url, $vars = []);
$response = $curl->get($url, $vars = []); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = []);
$response = $curl->put($url, $vars = []);
$response = $curl->delete($url, $vars = []);

To use a custom request methods, you can call the request method:, (*5)

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = []);

All of the built in request methods like put and get simply wrap the request method. For example, the post method is implemented like:, (*6)

function post($url, $vars = []) {
    return $this->request('POST', $url, $vars);
}

Examples:, (*7)

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the $curl->error() method., (*8)

The CurlResponse Object

A normal CURL request will return the headers and the body in one response string., (*9)

# Response Headers -------------------------------------------------------------------------

# Get the response body
echo $response->body(); # A string containing everything in the response except for the headers

# Get the response headers
print_r($response->headers()); # An associative array containing the response headers

# Pick one from response headers
echo $response->headers('Content-Type'); # text/html 

# You can also use those methods 
$response->status(); # 200 OK
$response->statusCode(); # 200
$response->ContentType(); # text/html

# Request Headers --------------------------------------------------------------------------

# Get the request headers
$response->requestHeaders(); # An associative array containing the request headers

# Pick one from request headers 
echo $response->requestHeaders('Version'); # HTTP/1.1

# Curl Information -------------------------------------------------------------------------
Get information regarding a specific transfer. See, http://php.net/manual/en/function.curl-getinfo.php

# Get all
$response->getInfo(); # An associative array containing the curl information

# Pick one
$response->getInfo('total_time'); # 0.14257

The CurlResponse class defines the magic __toString() method which will return the response body, so echo $response is the same as echo $response->body, (*10)

By default, cookies will be stored in a file called curl_cookie.txt. You can change this file's name by setting it like this, (*11)

$curl->setCookieFile('some_other_filename');

This allows you to maintain a session across requests, (*12)

Setting Custom Headers

You can set custom headers to send with the request, (*13)

$curl->setheader('SOME_KEY', 'some value');

# you can also method chaining
$response = $curl->setHeader('Content-Type', 'application/json')
     ->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux...')
     ->get('http://example.com');

Setting Custom CURL request options

By default, the Curl object will follow redirects. You can disable this by setting:, (*14)

$curl->setOptions('CURLOPT_FOLLOW_REDIRECTS', false);

You can set/override many different options for CURL requests (see the curl_setopt() documentation for a list of them), (*15)

Auth

Sets the user and password for HTTP auth basic authentication method., (*16)

$curl->setAuth('username', 'password');

Contact

Problems, comments, and suggestions all welcome: alioygur@gmail.com, (*17)

The Versions

05/11 2014

dev-master

9999999-dev

A basic CURL wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ali OYGUR

05/11 2014

v1.0

1.0.0.0

A basic CURL wrapper for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ali OYGUR