Files
php-curl-class/docs/source/index.md
T
2015-07-31 00:18:48 -07:00

4.3 KiB

title, language_tabs, toc_footers
title language_tabs toc_footers
PHP Curl Class Documentation
php
<a href="https://github.com/php-curl-class/php-curl-class" target="_blank">PHP Curl Class on Github</a>

Introduction

PHP Curl Class is an object-oriented wrapper of the PHP cURL extension.

Install

Composer

Install using Composer.

$ composer require php-curl-class/php-curl-class

Include

Include PHP Curl Class.

<?php
require 'Curl/Curl.php';

use \Curl\Curl;

Usage

GET

<?php
require 'Curl/Curl.php';

use \Curl\Curl;

$curl = new Curl();
$curl->get('https://www.example.com/', array(
    'q' => 'keyword',
));

POST

<?php
$curl = new Curl();
$curl->post('https://www.example.com/login/', array(
    'username' => 'myusername',
    'password' => 'mypassword',
));

PUT

<?php
$curl = new Curl();
$curl->put('https://api.example.com/user/', array(
    'first_name' => 'Zach',
    'last_name' => 'Borboa',
));

PATCH

<?php
$curl = new Curl();
$curl->patch('https://api.example.com/profile/', array(
    'image' => '@path/to/file.jpg',
));
<?php
$curl = new Curl();
$curl->patch('https://api.example.com/profile/', array(
    'image' => new CURLFile('path/to/file.jpg'),
));

DELETE

<?php
$curl = new Curl();
$curl->delete('https://api.example.com/user/', array(
    'id' => '1234',
));

HEAD

<?php
$curl = new Curl();
$curl->head('https://www.example.com/song.mp3');

OPTIONS

<?php
$curl = new Curl();
$curl->options('https://www.example.com/');

Curl Options

<?php
$curl = new Curl();
$curl->setOpt(CURLOPT_AUTOREFERER, true);
$curl->get('https://www.example.com/302');
<?php
$curl = new Curl();
// Enable gzip compression.
$curl->setOpt(CURLOPT_ENCODING , 'gzip');
$curl->get('https://www.example.com/image.png');

Authentication

<?php
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');

User Agent

<?php
$curl = new Curl();
$curl->setUserAgent('');

Referrer

<?php
$curl = new Curl();
$curl->setReferrer('');

Cookies

<?php
$curl = new Curl();
$curl->setCookie('key', 'value');

Request header

<?php
$curl = new Curl();
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->get('https://www.example.com/');

// Case-insensitive access to headers.
echo $curl->requestHeaders['X-Requested-With'] . "\n"; // XMLHttpRequest
echo $curl->requestHeaders['x-ReQUeStED-wiTH'] . "\n"; // XMLHttpRequest

Response header

<?php
$curl = new Curl();
$curl->get('https://www.example.com/image.png');

// Case-insensitive access to headers.
echo $curl->responseHeaders['Content-Type'] . "\n"; // image/png
echo $curl->responseHeaders['CoNTeNT-TyPE'] . "\n"; // image/png

Response body

<?php
$curl = new Curl();
$curl->get('https://www.example.com/');

echo $curl->response;

Curl object

Example access to curl object.

<?php
$curl->setOpt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
// or
curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
<?php
$curl->close();
// or
curl_close($curl->curl);

Errors

<?php
$curl = new Curl();
$curl->get('https://www.example.com/404');

if ($curl->error) {
    echo 'Error code: ' . $curl->errorCode . "\n"; // 404
    echo 'Error message: ' . $curl->errorMessage . "\n"; // HTTP/1.1 404 Not Found
}
else {
    echo $curl->response;
}

Parallel Requests

<?php
// Requests in parallel with callback functions.
$curl = new Curl();

$curl->success(function($instance) {
    echo 'call was successful. response was' . "\n";
    echo $instance->response . "\n";
});
$curl->error(function($instance) {
    echo 'call was unsuccessful.' . "\n";
    echo 'error code:' . $instance->errorCode . "\n";
    echo 'error message:' . $instance->errorMessage . "\n";
});
$curl->complete(function($instance) {
    echo 'call completed' . "\n";
});

$curl->get(array(
    'https://duckduckgo.com/',
    'https://search.yahoo.com/search',
    'https://www.bing.com/search',
    'http://www.dogpile.com/search/web',
    'https://www.google.com/search',
    'https://www.wolframalpha.com/input/',
), array(
    'q' => 'hello world',
));