jabranr/php-curl

A quick start PHP cURL library

2.0.0 2018-01-27 00:27 UTC

This package is auto-updated.

Last update: 2024-10-24 04:33:16 UTC


README

A simple PHP client for cURL operations.

Migrating from v1? Beware that v2 has breaking changes! Some of the API methods names have changed.

Simply import the library into your project. The best way to do so is to use Composer as following. Otherwise it can simply be downloaded from GitHub and added to the project.

$ composer require jabranr/php-curl

Start using it straight away. (Following example assumes that it was installed via Composer)

<?php

require 'path/to/vendor/autoload.php';

use Jabran\HttpUtil\HttpCurlRequest;
use Jabran\HttpUtil\Exception\HttpCurlException;

# Start new cURL request
$curl = new HttpCurlRequest('http://jabran.me');

Setting cURL options

<?php

# Set options - method 1
$curl->setOption(CURLOPT_RETURNTRANSFER, true);
$curl->setOption(CURLOPT_FOLLOWLOCATION, true);

# Set options - method 2
$curl->setOptions(array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true
));

# Execute request, get response and close connection
try {
    $response = $curl->execute();
} catch(HttpCurlException $e) {
    $curl->getErrorCode(); // -> cURL error number
    $curl->getErrorMessage(); // -> cURL error message
    
    # OR
    
    $e->getMessage(); // -> cURL error: [ErrorCode] ErrorMessage
}

# OR get more info and close connection manually
try {
    $response = $curl->execute(false);
} catch(HttpCurlException $e) { }

# Get response later
$response = $curl->getResponse();

# 
$info = $curl->getInfo();

# Close request
$curl->close();

API

The cURL class exposes following API:

getInfo

Get cURL request info. $option needs to be a valid cURL constant. If none given then it will return an associative array.

$curl->execute(false);
$curl->getInfo($option = null);
$curl->close();

getError

Get cURL request error message.

$curl->getError();

getErrorCode

Get cURL request error code.

$curl->getErrorCode();

getErrorMessage

Get cURL request error message.

$curl->getErrorMessage();

getHttpCode

Get cURL request HTTP status code.

$curl->getHttpCode();

getTotalTime

Get total time taken for a cURL request.

$curl->getTotalTime();

getResponse

Get response for a cURL request.

$curl->getResponse();

License

Feel free to use and send improvements via pull requests. Licensed under the MIT License.

© Jabran Rafique – 2016 – 2017