Coder Social home page Coder Social logo

php-guard / curl Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 81 KB

PHP Curl wrapper. Alternative to "https://github.com/php-curl-class/php-curl-class" with CurlRequest and CurlResponse objects

License: GNU General Public License v3.0

PHP 100.00%
php php7 curl curl-multi composer packagist http php-curl php-curl-library proxy

curl's Introduction

curl

Build Status Scrutinizer Code Quality Code Coverage GPL Licence

This library is an alternative to "https://github.com/php-curl-class/php-curl-class". Easy use with CurlRequest and CurlResponse objects.

Installation

Install via Composer

$ composer require php-guard/curl

Requirements

  • php: ^7.1

Usage

Quick Start
require __DIR__ . '/vendor/autoload.php';

use \PhpGuard\Curl\Curl;
use \PhpGuard\Curl\CurlError;

$this->curl  =  new  Curl();
	
try {
	// Execute a single request
	$response = $this->curl->get('http://example.com'); // Return a CurlResponse
	echo $response->raw();

	// Execute multiple requests
	$responses = $this->curl->multi() // Create a MultiCurl object
		->get('http://example.com')   // Add a get request
		->post('http://example.com')  // Add a post request
		->execute();                  // Return an array of CurlResponse

	foreach ($responses as $response) {
		echo $response->raw();
	}
} catch (CurlError $e) {
	echo 'Error: ' . $curl->getCode(). ': ' . $curl->getMessage(). "\n";
}
Available Methods

\PhpGuard\Curl\Curl

HTTP methods

get(string $url, $query = null, array $headers = []): CurlResponse
post(string $url, $data = null, $query = null, array $headers = []): CurlResponse
put(string $url, $data = null, $query = null, array $headers = []): CurlResponse
patch(string $url, $data = null, $query = null, array $headers = []): CurlResponse
delete(string $url, $data = null, $query = null, array $headers = []): CurlResponse

Run a single request

execute(CurlRequest  $request): CurlResponse

Prepare multiple requests

multi(array  $options  = []): MultiCurl

Edit request and response processing (advanced usage)

getCurlRequestFactory(): CurlRequestFactory
getRequestModifierPipeline(): RequestModifierPipeline

\PhpGuard\Curl\CurlRequest

You can use Curl to execute requests or you can use the CurlRequestFactory to return CurlRequest instances.

Properties url, method and data have getters and setters.

The method setHeaderContentType(string $contentType) is a shortcut for

$curlRequest->getHeaders['Content-Type'] = $contentType

Other methods

execute(bool $throwExceptionOnHttpError = false): CurlResponse
getCurlOptions(): CurlOptions
getHeaders(): Headers

\PhpGuard\Curl\MultiCurl

get(string $url, $query = null, array $headers = []): self
post(string $url, $data = null, $query = null, array $headers = []): self
put(string $url, $data = null, $query = null, array $headers = []): self
patch(string $url, $data = null, $query = null, array $headers = []): self
delete(string $url, $data = null, $query = null, array $headers = []): self
execute(): CurlResponse[]

\PhpGuard\Curl\CurlResponse

statusCode(): int
isError(): bool // True if status code >= 300
headers(): Headers
raw() // Raw content of the response
json() // Array or false if not a json response

\PhpGuard\Curl\CurlRequestFactory

Set the base url for all requests

 setBaseUrl(?string $baseUrl): self

Set the option curl CURLOPT_SSL_VERIFYPEER

 setSslVerifyPeer(bool $value): self

Create a CurlRequest

 create(string $method, string $url, $data = null, $query = null, array $headers = []): CurlRequest

Edit other default curl options

 getDefaultCurlOptions(): CurlOptions
 getDefaultHeaders(): Headers

\PhpGuard\Curl\Collection\CurlOptions

This class implements \ArrayAccess. It can therefore be used as an array.

\PhpGuard\Curl\Collection\Headers

This class implements \ArrayAccess. It can therefore be used as an array.

In addition, the keys are case insensitive.

\PhpGuard\Curl\RequestModifierPipeline

Add an object to modify CURL requests

pipe(RequestModifierInterface $requestModifier): self

By default, FileRequestModifier and PlainTextRequestModifier are active. If necessary, you can add an instance of ProxyRequestModifier

  • ProxyRequestModifier allows you to define curl options to use a proxy

  • FileRequestModifier is used to manage file paths starting with @ and passed as a parameter by transforming them into CurlFile and then modifying the HTTP Content-Type header.

  • PlainTextRequestModifier changes the HTTP Content-Type header to text/plain when a string is passed as a parameter.

curl's People

Contributors

alexandre-le-borgne avatar esynaps avatar

Watchers

 avatar  avatar

Forkers

esynaps

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.