This package is abandoned and no longer maintained. No replacement package was suggested.

A basic CURL wrapper for PHP. This is a modernised, and maintained, version of @shuber's rather nice, simple cURL wrapper, https://github.com/shuber/curl.

v2.0.3 2023-07-06 21:01 UTC

This package is auto-updated.

Last update: 2024-05-13 09:59:17 UTC


README

A basic cURL wrapper for PHP.

ℹ️ See https://www.php.net/curl for more information about the cURL extension for PHP

This fork is a modernised, and maintained, version of @shuber's rather nice, simple cURL wrapper.

Installation

Use Composer.

Usage

Initialization

Simply require and initialize the Curl class like so:

require '<your-project-dir>/vendor/autoload.php';

$curl = new PowderBlue\Curl\Curl();

Performing a Request

The Curl class provides shortcuts for making requests using the HEAD, GET, POST, PUT, and DELETE methods. You must always specify a URL; you can also pass an array/string of variables to send along with it, if need be.

// When making `HEAD` and `GET` requests, parameters will be appended to the URL in the form of a query-string
$response = $curl->head($url, $requestParams);
$response = $curl->get($url, $requestParams);
// Otherwise, you can pass an associative array or a string to pop into the body of the request
$response = $curl->post($url, $requestBody);
$response = $curl->put($url, $requestBody);
$response = $curl->delete($url, $requestBody);

Use Curl::request() to make a request using a custom request-method, thus:

$response = $curl->request('<method-name>', $url, $requestBody);

Examples:

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

// In this case, '?q=test' will be appended to the URL
$response = $curl->get('https://www.google.com/', ['q' => 'test']);

// The data will be encoded for you and the `Content-Type` header set to `multipart/form-data`
$response = $curl->post('test.com/posts', ['title' => 'Test', 'body' => 'This is a test']);

All requests return an instance of PowderBlue\Curl\Response if successful, or throw an exception if an error occurs.

The Response Class

A normal cURL request returns the headers and body in a single string. The PowderBlue\Curl\Response class splits that string, placing the two parts in separate properties.

For example:

$response = $curl->get('https://www.google.com/');
echo $response->body;
print_r($response->headers);

Would display something like:

<html>
<head>
<title>Google.com</title>
</head>
<body>
...
</body>
</html>

Array
(
    [Http-Version] => 1.0
    [Status-Code] => 200
    [Status] => 200 OK
    [Cache-Control] => private
    [Content-Type] => text/html; charset=ISO-8859-1
    [Date] => Wed, 07 May 2008 21:43:48 GMT
    [Server] => gws
    [Connection] => close
)

ℹ️ PowderBlue\Curl\Response::__toString() returns the response body, so—for example—echo $response will output the same as echo $response->body.

Cookies/Sessions

By default, cookies will be stored in <lib-dir>/var/curl_cookie.txt. You can change this by doing something like the following.

$curl->cookie_file = '<pathname>';

This allows you to maintain a session across requests.

Basic Configuration Options

You can easily set the referer or user-agent:

$curl->referer = '<url>';
$curl->user_agent = '<user-agent-string>';

Setting Headers

You can specify headers to send with the request:

$curl->headers['Host'] = 12.345.678.90;
$curl->headers['Custom-Header'] = 'foo';
$curl->headers['User-Agent'] = '<user-agent-string>';

Setting Custom cURL Request Options

By default, redirects will be followed. You can disable this with:

$curl->follow_redirects = false;

If you need to do something a little more exotic, you can set/override cURL options like this:

$curl->options[CURLOPT_AUTOREFERER] = true;

ℹ️ See the curl_setopt() documentation for a list of cURL request options

Get in Touch

Problems, comments, and suggestions are all welcome: support@powder-blue.com.