PHP Classes

PHP CURL Component: Compose and execute HTTP requests with Curl

Recommend this page to a friend!
  Info   View files Example   View files View files (143)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStarStar 80%Total: 649 This week: 1All time: 4,937 This week: 560Up
Version License PHP version Categories
php-curl-component 0.14.10GNU Lesser Genera...5.3HTTP, PHP 5
Description 

Author

This package can compose and execute HTTP requests with Curl.

It provides a fluent interface to define several parameters of a HTTP request to be sent to a given URL using the Curl library.

Currently it provides means to define the request URL, request method (POST, GET, DELETE, PATCH and PUT), request parameter values, timeout values.

Other calls can tell the package to execute the request and retrieve the response.

Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 150195 in Germany Germany
Week rank: 106 Up5 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Recommendations

Link Checker
Find broken links in a Web site

Example

#!/usr/bin/env php
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-12-10
 */

require_once __DIR__ . '/../vendor/autoload.php';

use
Net\Bazzline\Component\Curl\Builder\BuilderFactory;
use
Net\Bazzline\Component\Curl\Dispatcher\LoggingDispatcher;

$factory = new BuilderFactory();
$factory->overwriteDispatcher(new LoggingDispatcher());
$builder = $factory->create();
$url = ($argc > 1) ? $argv[1] : 'https://httpbin.org/get';

$response = $builder->onTheUrl($url)
    ->
usePost()
    ->
andFetchTheResponse();

echo
'content: ' . $response->content() . PHP_EOL;
echo
'content type: ' . $response->contentType() . PHP_EOL;
echo
'head lines: ' . var_export($response->headerLines(), true) . PHP_EOL;
echo
'error:' . $response->error() . PHP_EOL;
echo
'error code:' . $response->errorCode() . PHP_EOL;
echo
'status code: ' . $response->statusCode() . PHP_EOL;


Details

Simple Curl Wrapper Component for PHP

This project aims to deliver an easy to use and free as in freedom object oriented php curl command component.

The build status of the current master branch is tracked by Travis CI: Build Status Latest stable

The scrutinizer status is: code quality

The versioneye status is: Dependency Status

Take a look on openhub.net.

The current change log can be found here.

This component is not developed to replace guzzle.

Example

By Using The Builder

//it is always good to ship the component with a factory to easy up usage
use Net\Bazzline\Component\Curl\BuilderFactory;
use Net\Bazzline\Component\Curl\Option\Timeout;

$factory    = new BuilderFactory();
$builder    = $factory->create();
$url        = 'http://www.foo.bar';
$timeout    = new Timeout(10);  //set the timeout to 10 seconds

/
 * you can also use:
 *  //assuming that $dispatcher is an instance of DispatcherInterface
 *  //assuming that $requestFactory is an instance of RequestFactory
 *  $builder->overwriteDispatcher($dispatcher);
 *  $builder->overwriteRequestFactory($requestFactory);
 */

$response = $builder->usePost()
    ->onTheUrl($url)
    ->withTheData($data)
    ->withTheParameters(array('descendingOrderBy' => 'id'))
    ->withTheOption($timeout)
    ->andFetchTheResponse();

/ 
 * you can also use:
 *  $builder->withTheHeaderLine($headLine)    //add the headline you want
 *  $builder->withResponseModifier($modifier) //add the response modifier you want
 */

echo 'content: ' . $response->content() . PHP_EOL;
echo 'content type: ' . $response->contentType() . PHP_EOL;
echo 'error:' . $response->error() . PHP_EOL;
echo 'error code:' . $response->errorCode() . PHP_EOL;
echo 'head lines: ' . var_export($response->headerLines(), true) . PHP_EOL;
echo 'status code: ' . $response->statusCode() . PHP_EOL;

By Using The Request

//it is always good to ship the component with a factory to easy up usage
$factory    = new Net\Bazzline\Component\Curl\RequestFactory();
$request    = $factory->create();
$url        = 'http://www.foo.bar';

$response = $request->get($url);

echo 'content: ' . $response->content() . PHP_EOL;
echo 'content type: ' . $response->contentType() . PHP_EOL;
echo 'error:' . $response->error() . PHP_EOL;
echo 'error code:' . $response->errorCode() . PHP_EOL;
echo 'head lines: ' . var_export($response->headerLines(), true) . PHP_EOL;
echo 'status code: ' . $response->statusCode() . PHP_EOL;

Executable Examples

More Examples

Post Request With Basic Authentication

//begin of runtime environments
$factory        = new BuilderFactory();
$builder        = $factory->create();
$data           = array(
    'there'     => 'is',
    'no'        => 'foo',
    'without'   => 'a bar'
);
$password       = '<super secret password>';
$username       = 'foo@bar.ru';
$url            = 'https://foo.bar.ru/api/my/rest/endpoint/v1';
//end of runtime environments

//begin building the request
$builder->asJson();
$builder->onTheUrl($url);
$builder->withTheData($data);
$builder->withTheOption(new SetBasicAuthentication());
$builder->withTheOption(new SetUsernameAndPassword($username, $password));
$builder->usePost();
//end building the request

$request = $builder->andFetchTheResponse();
echo PHP_EOL . 'dumping the request' . PHP_EOL;
var_dump($request);

Terms

  • Dispatcher * doing the curl request * if you want to use pure curl, use this class
  • Request * object oriented approach reflecting the request * HeadLine * object oriented http headers (list of available headers), start a pull request if you need more * Options * object oriented curl options, start a pull request if you need more * Parameters * all the parameters you want to add to your url - they are urlencoded automatically * Url * the url of your endpoint
  • Response * object oriented approach reflecting the response * ResponseBehaviour * interface to interact with the response * either modify the response (by creating a new one) * change the flow by throwing an exception if the response does not fits your needs (as example)
  • Builder * provides a fluent interface to easy up using curl * it takes care of all :-)

Not Available Curl Options

In general, the php version is limiting the available curl options. Furthermore, some options are not implemented because of their hardcoded usage in the Response or the Dispatcher (you can set it but they would be overwritten).

This options are:

  • used in the Request * CURLOPT_CUSTOMREQUEST * CURLOPT_HTTPHEADER * CURLOPT_POSTFIELDS
  • used in the Dispatcher * CURLINFO_HEADER_OUT * CURLOPT_HEADERFUNCTION * CURLOPT_RETURNTRANSFER

If you want to change this, you either have to extend the existing Request or Dispatcher object.

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_curl
cd vendor/net_bazzline/php_component_curl
git clone https://github.com/bazzline/php_component_curl .

With Packagist

composer require net_bazzline/php_component_curl:dev-master

Links

  • http://resttesttest.com/

Other Components Available

  • https://github.com/php-mod/curl
  • https://github.com/anlutro/php-curl
  • https://github.com/hamstar/curl
  • https://github.com/jyggen/curl
  • https://github.com/ixudra/Curl
  • https://github.com/brodkinca/BCA-PHP-CURL
  • https://github.com/miliqi/laravel-curl
  • https://github.com/andrefigueira/Lib-Curl

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it. Donate something if you love it :-].


  Files folder image Files  
File Role Description
Files folder imageexample (5 files)
Files folder imagesource (1 file, 7 directories)
Files folder imagetest (2 files, 4 directories)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file make_a_delete_request Example Example
  Accessible without login Plain text file make_a_get_request Example Example script
  Accessible without login Plain text file make_a_patch_request Example Example script
  Accessible without login Plain text file make_a_post_request Example Example script
  Accessible without login Plain text file make_a_put_request Example Example script

  Files folder image Files  /  source  
File Role Description
Files folder imageBuilder (2 files)
Files folder imageDispatcher (3 files)
Files folder imageHeaderLine (9 files)
Files folder imageOption (11 files, 8 directories)
Files folder imageRequest (2 files)
Files folder imageResponse (1 file)
Files folder imageResponseBehaviour (3 files)
  Plain text file FactoryInterface.php Class Class source

  Files folder image Files  /  source  /  Builder  
File Role Description
  Plain text file Builder.php Class Class source
  Plain text file BuilderFactory.php Class Class source

  Files folder image Files  /  source  /  Dispatcher  
File Role Description
  Plain text file Dispatcher.php Class Class source
  Plain text file DispatcherInterface.php Class Class source
  Plain text file LoggingDispatcher.php Class Class source

  Files folder image Files  /  source  /  HeaderLine  
File Role Description
  Plain text file AbstractContentType.php Class Class source
  Plain text file AbstractHeaderLine.php Class Class source
  Plain text file AcceptEncoding.php Class Class source
  Plain text file AcceptLanguage.php Class Class source
  Plain text file ContentTypeIsUtf8Form.php Class Class source
  Plain text file ContentTypeIsUtf8Html.php Class Class source
  Plain text file ContentTypeIsUtf8Json.php Class Class source
  Plain text file Custom.php Class Class source
  Plain text file HeaderLineInterface.php Class Class source

  Files folder image Files  /  source  /  Option  
File Role Description
Files folder imageAuthentication (4 files)
Files folder imageAuthorization (1 file)
Files folder imageBehaviour (25 files)
Files folder imageCallback (4 files)
Files folder imageCookie (4 files)
Files folder imageFtp (10 files)
Files folder imageSecurity (15 files)
Files folder imageTransfer (28 files)
  Plain text file AbstractAuthentication.php Class Class source
  Plain text file AbstractSetOptionArrayValue.php Class Class source
  Plain text file AbstractSetOptionClosureValue.php Class Class source
  Plain text file AbstractSetOptionIntValue.php Class Class source
  Plain text file AbstractSetOptionMixedValue.php Class Class source
  Plain text file AbstractSetOptionStreamValue.php Class Class source
  Plain text file AbstractSetOptionStringValue.php Class Class source
  Plain text file AbstractSetOptionToFalse.php Class Class source
  Plain text file AbstractSetOptionToTrue.php Class Class source
  Plain text file OptionInterface.php Class Class source
  Plain text file SetOption.php Class Class source

  Files folder image Files  /  source  /  Option  /  Authentication  
File Role Description
  Plain text file EnableUnrestrictedAuth.php Class Class source
  Plain text file SetBasicAuthentication.php Class Class source
  Plain text file SetKeyPassword.php Class Class source
  Plain text file SetUsernameAndPassword.php Class Class source

  Files folder image Files  /  source  /  Option  /  Authorization  
File Role Description
  Plain text file EnableNetrc.php Class Class source

  Files folder image Files  /  source  /  Option  /  Behaviour  
File Role Description
  Plain text file DisableBody.php Class Class source
  Plain text file DisableProgress.php Class Class source
  Plain text file DisableSignal.php Class Class source
  Plain text file EnableAutoReferer.php Class Class source
  Plain text file EnableFailOnError.php Class Class source
  Plain text file EnableFollowAllocation.php Class Class source
  Plain text file EnableForbidReuse.php Class Class source
  Plain text file EnableFreshConnect.php Class Class source
  Plain text file EnableMute.php Class Class source
  Plain text file EnableVerbose.php Class Class source
  Plain text file SetConnectTimeOutInMilliSeconds.php Class Class source
  Plain text file SetConnectTimeOutInSeconds.php Class Class source
  Plain text file SetDnsCacheTimeout.php Class Class source
  Plain text file SetHttp200Aliases.php Class Class source
  Plain text file SetLowSpeedLimit.php Class Class source
  Plain text file SetMaxConnects.php Class Class source
  Plain text file SetMaxRedirs.php Class Class source
  Plain text file SetRange.php Class Class source
  Plain text file SetRedirProtocols.php Class Class source
  Accessible without login Plain text file SetResumeFrom.php Example Example script
  Plain text file SetStderr.php Class Class source
  Plain text file SetTcpNoDelay.php Class Class source
  Plain text file SetTimeCondition.php Class Class source
  Plain text file SetTimeOutInMilliSeconds.php Class Class source
  Plain text file SetTimeOutInSeconds.php Class Class source

  Files folder image Files  /  source  /  Option  /  Callback  
File Role Description
  Plain text file SetCallbackForPassWordFunction.php Class Class source
  Plain text file SetCallbackForProgressFunction.php Class Class source
  Plain text file SetCallbackForReadFunction.php Class Class source
  Plain text file SetCallbackForWriteFunction.php Class Class source

  Files folder image Files  /  source  /  Option  /  Cookie  
File Role Description
  Plain text file EnableCookieSession.php Class Class source
  Plain text file SetCookie.php Class Class source
  Plain text file SetCookieFile.php Class Class source
  Plain text file SetCookieJar.php Class Class source

  Files folder image Files  /  source  /  Option  /  Ftp  
File Role Description
  Plain text file EnableFtpAppend.php Class Class source
  Plain text file EnableFtpAscii.php Class Class source
  Plain text file EnableFtpCreateMissingDirs.php Class Class source
  Plain text file EnableFtpListOnly.php Class Class source
  Plain text file EnableFtpUseEprt.php Class Class source
  Plain text file EnableFtpUseEpsv.php Class Class source
  Plain text file SetFtpPort.php Class Class source
  Plain text file SetFtpSslAuth.php Class Class source
  Plain text file SetPostQuote.php Class Class source
  Plain text file SetQuote.php Class Class source

  Files folder image Files  /  source  /  Option  /  Security  
File Role Description
  Plain text file DisableSslVerifyHost.php Class Class source
  Plain text file DisableSslVerifyPeer.php Class Class source
  Plain text file EnableCertInfo.php Class Class source
  Plain text file SetCaInfo.php Class Class source
  Plain text file SetCaPath.php Class Class source
  Plain text file SetSslCert.php Class Class source
  Plain text file SetSslCertPasswd.php Class Class source
  Plain text file SetSslCertType.php Class Class source
  Plain text file SetSslCipherList.php Class Class source
  Plain text file SetSslEngine.php Class Class source
  Plain text file SetSslEngineDefault.php Class Class source
  Plain text file SetSslKey.php Class Class source
  Plain text file SetSslKeyPasswd.php Class Class source
  Plain text file SetSslKeyType.php Class Class source
  Plain text file SetSslVersion.php Class Class source

  Files folder image Files  /  source  /  Option  /  Transfer  
File Role Description
  Plain text file DisableDnsUseGlobalCache.php Class Class source
  Plain text file EnableBinaryTransfer.php Class Class source
  Plain text file EnableCrlf.php Class Class source
  Plain text file EnableFileTime.php Class Class source
  Plain text file EnableHeader.php Class Class source
  Plain text file EnableHttpProxyTunnel.php Class Class source
  Plain text file EnableSafeUpload.php Class Class source
  Plain text file EnableTransferText.php Class Class source
  Plain text file EnableUpload.php Class Class source
  Plain text file SetBufferSize.php Class Class source
  Plain text file SetEgdSocket.php Class Class source
  Plain text file SetEncoding.php Class Class source
  Plain text file SetFile.php Class Class source
  Plain text file SetHttpVersion.php Class Class source
  Plain text file SetInFile.php Class Class source
  Plain text file SetInFileSize.php Class Class source
  Plain text file SetInterface.php Class Class source
  Plain text file SetPort.php Class Class source
  Plain text file SetProxy.php Class Class source
  Plain text file SetProxyAuth.php Class Class source
  Plain text file SetProxyPort.php Class Class source
  Plain text file SetProxyType.php Class Class source
  Plain text file SetProxyUserPwd.php Class Class source
  Plain text file SetRandomFile.php Class Class source
  Plain text file SetReferer.php Class Class source
  Plain text file SetTimeValue.php Class Class source
  Plain text file SetUserAgent.php Class Class source
  Plain text file SetWriteHeader.php Class Class source

  Files folder image Files  /  source  /  Request  
File Role Description
  Plain text file Request.php Class Class source
  Plain text file RequestFactory.php Class Class source

  Files folder image Files  /  source  /  Response  
File Role Description
  Plain text file Response.php Class Class source

  Files folder image Files  /  source  /  ResponseBehaviour  
File Role Description
  Plain text file ConvertJsonToArrayBehaviour.php Class Class source
  Plain text file ResponseBehaviourInterface.php Class Class source
  Plain text file ThrowRuntimeExcept...eLimitBehaviour.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imageBuilder (1 file)
Files folder imageRequest (1 file)
Files folder imageResponse (1 file)
Files folder imageResponseBehaviour (2 files)
  Accessible without login Plain text file AbstractTestCase.php Test Unit test script
  Accessible without login Plain text file bootstrap.php Test Unit test script

  Files folder image Files  /  test  /  Builder  
File Role Description
  Accessible without login Plain text file BuilderTest.php Test Unit test script

  Files folder image Files  /  test  /  Request  
File Role Description
  Accessible without login Plain text file RequestTest.php Test Unit test script

  Files folder image Files  /  test  /  Response  
File Role Description
  Accessible without login Plain text file ResponseTest.php Test Unit test script

  Files folder image Files  /  test  /  ResponseBehaviour  
File Role Description
  Accessible without login Plain text file ConvertJsonToArrayBehaviourTest.php Test Unit test script
  Accessible without login Plain text file ThrowRuntimeExcept...itBehaviourTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:649
This week:1
All time:4,937
This week:560Up
 User Ratings  
 
 All time
Utility:90%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:90%StarStarStarStarStar
Examples:90%StarStarStarStarStar
Tests:85%StarStarStarStarStar
Videos:-
Overall:80%StarStarStarStarStar
Rank:14