• Stars
    star
    245
  • Rank 165,304 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple PHP curl wrapper class

php-curl

Build Status Latest Stable Version Latest Unstable Version License

The smallest possible OOP wrapper for PHP's curl capabilities.

Note that this is not meant as a high-level abstraction. You should still know how "pure PHP" curl works, you need to know the curl options to set, and you need to know some HTTP basics.

If you're looking for a more user-friendly abstraction, check out Guzzle.

Installation

$ composer require anlutro/curl

Usage

$curl = new anlutro\cURL\cURL;

$response = $curl->get('http://www.google.com');

// easily build an url with a query string
$url = $curl->buildUrl('http://www.google.com', ['s' => 'curl']);
$response = $curl->get($url);

// the post, put and patch methods takes an array of POST data
$response = $curl->post($url, ['api_key' => 'my_key', 'post' => 'data']);

// add "json" to the start of the method to convert the data to a JSON string
// and send the header "Content-Type: application/json"
$response = $curl->jsonPost($url, ['post' => 'data']);

// if you don't want any conversion to be done to the provided data, for example
// if you want to post an XML string, add "raw" to the start of the method
$response = $curl->rawPost($url, '<?xml version...');

// raw request are also the easiest way to upload files
$file = curl_file_create('/path/to/file');
$response = $curl->rawPost($url, ['file' => $file]);

// a response object is returned
var_dump($response->statusCode); // response status code integer (for example, 200)
var_dump($response->statusText); // full response status (for example, '200 OK')
echo $response->body;
var_dump($response->headers); // array of headers
var_dump($response->info); // array of curl info

If you need to send headers or set cURL options you can manipulate a request object directly. send() finalizes the request and returns the result.

// newRequest, newJsonRequest and newRawRequest returns a Request object
$request = $curl->newRequest('post', $url, ['foo' => 'bar'])
	->setHeader('Accept-Charset', 'utf-8')
	->setHeader('Accept-Language', 'en-US')
	->setOption(CURLOPT_CAINFO, '/path/to/cert')
	->setOption(CURLOPT_FOLLOWLOCATION, true);
$response = $request->send();

You can also use setHeaders(array $headers) and setOptions(array $options) to replace all the existing options.

Note that some curl options are reset when you call send(). Look at the source code of the method cURL::prepareMethod for a full overview of which options are overwritten.

HTTP basic authentication:

$request = $curl->newRequest('post', $url, ['foo' => 'bar'])
	->setUser($username)->setPass($password);

Laravel

The package comes with a facade you can use if you prefer the static method calls over dependency injection.

You do not need to add a service provider.

Optionally, add 'cURL' => 'anlutro\cURL\Laravel\cURL' to the array of aliases in config/app.php.

Replace $curl-> with cURL:: in the examples above.

Contact

Open an issue on GitHub if you have any problems or suggestions.

License

The contents of this repository is released under the MIT license.

More Repositories

1

laravel-settings

Persistent settings in Laravel
PHP
940
star
2

laravel-4-smart-errors

Smarter error handling for Laravel 4
PHP
65
star
3

laravel-repository

Pseudo-repositories on top of Laravel's ORM
PHP
62
star
4

php-bulk-sms

BulkSMS API - PHP implementation
PHP
36
star
5

laravel-4-core

UNMAINTAINED! Platform for apps on top of Laravel 4
PHP
26
star
6

phint

PHP code analyzer
PHP
23
star
7

php-menu

PHP Dynamic menu builder
PHP
20
star
8

laravel-testing

Laravel 4 Test classes
PHP
13
star
9

php-form

Dynamic form builder for PHP
PHP
13
star
10

access

UNMAINTAINED! Laravel 4 RBAC
PHP
13
star
11

laravel-validation

Laravel Validation service class library
PHP
11
star
12

dotfiles

All my personal Linux configuration files and scripts.
Shell
7
star
13

botologist

Plugin-driven Python3 IRC
Python
5
star
14

laravel-controller

Laravel 4 Controller classes
PHP
5
star
15

sc2ranks-v2

SC2Ranks v2 API implementation
PHP
5
star
16

phpbench

UNMAINTAINED! Check out polyfractal's Athletic instead
PHP
3
star
17

russell

Python 3 static HTML site generator, focused on blogging, and programmability instead of configurability.
Python
3
star
18

psm

Python Script Manager
Shell
3
star
19

laravel-presenter

Laravel 4 Presenter class
PHP
3
star
20

python-project-examples

Check out the different branches for variations on Python project structures.
Python
2
star
21

diay.py

Dependency injection library for Python.
Python
2
star
22

blog

Build script and conent for lutro.me
Jinja
1
star
23

fjell.py

A rock-solid Python web framework for those who aren't afraid of some heavy lifting and steep learning curves.
Python
1
star
24

multidocs

Generate documentation from multiple sources.
Python
1
star
25

fpm-docker-example

Example of how to build deb/rpm packages using FPM in Docker.
Shell
1
star
26

venv.sh

Conveniently manage virtualenvs in your shell.
Shell
1
star
27

qdb

IRC quote database
Python
1
star