Simple PHP curl wrapper class

Related tags

HTTP php-curl
Overview

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.

You might also like...
Online tool to convert `curl` requests to Laravel `Http` requests

curl Converter Another bit of automation from Shift to convert curl requests to Laravel Http requests. This project is a WIP. You may follow along wit

A simple OOP wrapper to work with HTTP headers in PHP

Headers This package is to allow you to create HTTP Headers in PHP, in a simple and reliable way. Installation composer require http-php/headers Usage

Php Cookie Class

Security Simple and Light Cookie Class for Php Features Create, Delete, Set Cookie Set Prefix, Domain, Secure, OnlyHttp, Path Install run the followin

Simple handler system used to power clients and servers in PHP (this project is no longer used in Guzzle 6+)

RingPHP Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function. RingPHP be used to power HTTP clie

A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.
A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.

Brutal A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to apply brute force attack via the http protocol. Bru

A functional and simple rate limit control to prevent request attacks ready-to-use for PHP.

RateLimitControl A functional and simple rate limit control to prevent request attacks ready-to-use for PHP. Features: Prepared statements (using PDO)

Zenscrape package is a simple PHP HTTP client-provider that makes it easy to parsing site-pages

Zenscrape package is a simple PHP HTTP client-provider that makes it easy to parsing site-pages

A simple script i made that generate a valid http(s) proxy in json format with its geo-location info

Gev Proxy Generator GPG is a simple PHP script that generate a proxy using free services on the web, the proxy is HTTP(s) and it generate it in json f

Satis Control Panel (SCP) is a simple web UI for managing your Satis Repository for Composer Packages.

Satis Control Panel Satis Control Panel (SCP) is a simple web UI for managing your Satis Repository for Composer Packages. SCP backend is written in L

Comments
  • Implement interface?

    Implement interface?

    It would be wonderful if you could have your cURL class implement some interface.

    Right now, if I want to type-hint the type of a class method argument, I'm forced to type hint the concrete class. If the concrete class would implement an interface, I could type-hint the interface instead. If I can type-hint an interface, I can more easily supply a mock object for unit testing purposes.

    enhancement code quality help wanted 
    opened by Pixelfck 2
Releases(1.5.1)
  • 1.5.1(Jun 14, 2021)

  • 1.5.0(Jun 1, 2021)

  • 1.4.8(Jan 26, 2021)

  • 1.4.7(Dec 14, 2018)

  • 1.4.6(Nov 19, 2018)

  • 1.4.5(Oct 31, 2018)

  • 1.4.4(Oct 31, 2018)

  • 1.4.3(May 5, 2017)

  • 1.4.2(Mar 23, 2016)

  • 1.4.1(Dec 10, 2015)

  • 1.4.0(Oct 24, 2015)

    Added curl->setDefaultHeaders(array $headers) and curl->setDefaultHeader($key, $value) for headers that should be set on every request.

    Added curl->setDefaultOptions(array $options) and curl->setDefaultOption($key, $value) for curl_setopt calls that should be done on every request.

    Minor improvements to header consistency.

    Fixed some @return docblocks.

    Source code(tar.gz)
    Source code(zip)
  • 1.3.3(Jul 30, 2015)

  • 1.3.2(Jul 30, 2015)

    Improved parsing of HTTP status code header, allowing HTTP 100 Continue responses to be parsed appropriately. See #25

    Response headers are now parsed correctly when there are more than 2 duplicate headers. See https://github.com/anlutro/php-curl/issues/25#issuecomment-126240010

    Source code(tar.gz)
    Source code(zip)
  • 1.3.1(Jul 27, 2015)

  • 1.3.0(Jul 20, 2015)

    If a response contains multiple headers of the same type (common for cache-control, for example), $response->getHeader() will now return an array of headers instead of just the last occurance of the header. #21

    Headers are now stored case-insensitively internally, so it's no longer possible to set duplicate headers if they vary in casing.

    Can now do HTTP HEAD requests.

    You can now get the request that triggered a cURLException:

    try { /* ... */ } catch (cURLException $e) {
        var_dump($e->getRequest());
    }
    
    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Jul 13, 2015)

    • Added Request::setCookie($key, $value) for easily setting multiple cookies.
    • Added Request::auth($username, $password) to make HTTP basic authentication a bit less verbose.
    Source code(tar.gz)
    Source code(zip)
  • 1.1.1(Jul 13, 2015)

  • 1.1.0(Jul 7, 2015)

    The library now throws a cURLException with an improved error message instead of a RuntimeException if there was an error during the curl request.

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Jul 7, 2015)

Owner
Andreas Lutro
Software, systems, cloud, devops engineer. Living in Amsterdam, originally from Norway.
Andreas Lutro
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs

PHP Curl Class: HTTP requests made easy PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs. Installation Requirements Quic

null 3.1k Jan 5, 2023
This library provides an object-oriented wrapper of the PHP cURL extension

PHP Curl Class This library provides an object-oriented wrapper of the PHP cURL extension. If you have questions or problems with installation or usag

PHP MOD 321 Dec 30, 2022
Simple HTTP cURL client for PHP 7.1+ based on PSR-18

Simple HTTP cURL client for PHP 7.1+ based on PSR-18 Installation composer require sunrise/http-client-curl QuickStart composer require sunrise/http-f

Sunrise // PHP 15 Sep 5, 2022
PHP Curl ile letgo api kütüphanesi oluşturuldu. php ile letgo giriş yap.

Kendi LETGO API ile işlemler gerçekleştirelim. // email işlemleri $server = 'imap.gmail.com'; $user = '[email protected]'; $pass = 'password'; $port = 9

Görkem Bayraktar 2 Nov 3, 2022
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.

Httpful Httpful is a simple Http Client library for PHP 7.2+. There is an emphasis of readability, simplicity, and flexibility – basically provide the

Nate Good 1.7k Dec 21, 2022
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Ares 431 Dec 12, 2022
Custom PHP curl library for the Laravel 5 framework - developed by Ixudra

ixudra/curl Custom PHP cURL library for the Laravel 4 or 5 framework - developed by Ixudra. The package provides an easy interface for sending cURL re

Jan Oris 556 Jan 6, 2023
PHP cURL for feed Instagram Graph API

php-curl-instagram-graph PHP cURL for feed Instagram Graph API Script made based on the new (2020) Instagram API that requires authorization token gen

null 12 Apr 13, 2022
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Ares 431 Dec 12, 2022
Plug & Play [CURL + Composer Optional], Proxy as a Service, Multi-tenant, Multi-Threaded, with Cache & Article Spinner

?? .yxorP The SAAS(y), Multitenancy & Augmenting Web Proxy Guzzler is a 100% SAAS(y) plug-and-play (PHP CURL+Composer are Optional) solution that leverages SAAS architecture to provide multi-tenancy, multiple threads, caching, and an article spinner service.

4D/ҵ.com Dashboards 12 Nov 17, 2022