PHP Classes
elePHPant
Icontem

PHP CURL Component: Compose and execute HTTP requests with Curl

Recommend this page to a friend!
  Info   View files Example   View files View files (142)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-04-20 (2 days ago) RSS 2.0 feedStarStarStarStarStar 86%Total: 347 This week: 26All time: 6,701 This week: 31Up
Version License PHP version Categories
php-curl-component 0.14.7GNU 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.

  Performance   Level  
Name: nvb <contact>
Classes: 5 packages by
Country: Germany Germany
Age: ???
All time rank: 2517162 in Germany Germany
Week rank: 26 Up2 in Germany Germany Up
Innovation award
Innovation award
Nominee: 2x

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.

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

History

  • upcomming * @todo * to discuss * add dispatcher or HandlerGenerator/HandlerFactory * https://secure.php.net/manual/en/function.curl-init.php * https://secure.php.net/manual/en/function.curl-multi-init.php * add RequestModifier * e.g. for adding the JsonModifier which converts the data into a json, adds the fitting ContentType etc. * add support for parallel request execution like in the stil/curl library * replace current dispatcher and logging dispatcher strategy with an event driven approach (currently only needed for logging)? * create Request Data Domain Object * create Request Options Domain Object
  • 0.15.1 - released at 29.03.2017 * moved phpunit dependency to 5.7
  • 0.15.0 - released at 27.10.2016 * added support to send data when calling http method DELETE
  • 0.14.4 - released at 20.09.2016 * added AcceptLanguate header line
  • 0.14.3 - released at 19.09.2016 * added AcceptEncoding header line * added Custom header line * started more examples section updated phpunit to 5.5.
  • 0.14.2 - released at 30.05.2016 * relaxed dependency to mockery
  • 0.14.1 - released at 11.03.2016 removed.php* for examples and made them executable * updated dependencies
  • 0.14.0 - released at 26.02.2016 removed default header "application/x-www-form-urlencoded; charset=UTF-8" and added newContentTypeUtf8Html*
  • 0.13.0 - released at 17.02.2016 * fixed "ConverteJsonToArayBehaviour" by merging pull request 3
  • 0.12.0 - released at 08.02.2016 addedcurl_close($handler)in defaultDispatcher* fixed bug inBuilderFactory::createRequestFromFactory()* fixed bug inRequestFactory::create()*
  • 0.11.0 - released at 08.02.2016 fixed major bug increateRequestFromFactorytoBuilderFactory* * refactored internals of Builder::andFetchTheResponse() * refactored internals of Request::execute() * refactored internals of RequestFactory::create()
  • 0.10.0 - released at 08.02.2016 added publicoverwriteRequestFactory()toBuilderFactory* * fixed broken links in the readme moved topsr-4* autoloading * removed dedicated integration test for php 5.3.3
  • 0.9.1 - released at 14.01.2016 * updated dependencies
  • 0.9.0 - released at 05.01.2016 renamedEnableSslVerifyPeertoDisableSslVerifyPeer* renamedSetSslVerifyHosttoDisableSslVerifyHost*
  • 0.8.0 - released at 05.01.2016 Response::headerLine()is now throwing an\InvalidArgumentExceptioninstead of returningnullwhen a not existing$prefix* is provided
  • 0.7.0 - released at 21.12.2015 addedoverwriteDispatchermethod intoBuilderFactoryandRequestFactory* createdLoggingDispatcher* (basic standard out logging - you can overwrite this behaviour) implemented usage ofLoggingDispatcher* in the examples movedBuilder* into own namespace movedDispatcher* into own namespace movedRequest* into own namespace movedResponse* into own namespace refactoredRequestFactory* by extracting all header lines or options into protected methods renamedHeadLineintoHeaderLine*
  • 0.6.1 - released at 18.12.2015 addedBuilder::withTheRawHeaderLine()andBuilder::withTheRawOption()*
  • 0.6.0 - released at 18.12.2015 addedNot Available Curl Options* section in the readme moved setting of curl option "CURLOPT_RETURNTRANSFER" fromRequestinto the defaultDispatcher* rearrangedResponse* constructor parameters
  • 0.5.0 - released at 17.12.2015 addedResponse::headerLines()andResponse::headerLine($prefix)* and removed header output from the content
  • 0.4.1 - released at 16.12.2015 * add Request::options(), Request::trace() and Request::head() * fixed RequestFactory
  • 0.4.0 - released at 16.12.2015 * added unit test for * Builder * ConvertJsonToArrayBehaviour * ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviour * fixed issue in Builder
  • 0.3.0 - released at 16.12.2015 * fixed issue in BasicAuthentication HeadLine * fixed issue in builder example * fixed issue in Request when dealing with data that is not an array * fixed linking to DispatcherInterface * fixed style in DispatcherInterface * added unit test for * request * added curl options
  • 0.2.0 - released at 14.12.2015 * fixed issue in Response::contentType() * fixed styles * added dispatcher interface * added scrutinizer, travis-ci and version eye status * added support for scrutinizer and travis-ci * added support for url as optional argument in example scripts * added unit test for * response
  • 0.1.2 - released at 10.12.2015 * adapted behaviour to support the "error" in the response * extended terms
  • 0.1.1 - released at 10.12.2015 * add description to terms * add installation howto * add link to examples
  • 0.1.0 - released at 10.12.2015 * initial plumber release

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 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:347
This week:26
All time:6,701
This week:31Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:91%StarStarStarStarStar
Videos:-
Overall:86%StarStarStarStarStar
Rank:2