PHP CURL Component: Compose and execute HTTP requests with Curl

Recommend this page to a friend!
  Info   Example   View files (143)   Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
82%Total: 566 All time: 5,320 This week: 308
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.

Recommendations

Link Checker
Find broken links in a Web site

Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 10 packages by
Country: Germany Germany
Age: ???
All time rank: 1809112 in Germany Germany
Week rank: 713 32 in Germany Germany
Innovation award
Innovation award
Nominee: 6x

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  
File Role Description
example (5 files)
source (1 file, 7 directories)
test (2 files, 4 directories)
.gitignore Data Auxiliary data
.scrutinizer.yml Data Auxiliary data
.travis.yml Data Auxiliary data
CHANGELOG.md Data Auxiliary data
composer.json Data Auxiliary data
LICENSE Lic. License
phpunit.xml.dist Data Auxiliary data
README.md Doc. Documentation

  Files  /  example  
File Role Description
   make_a_delete_request Example Example
   make_a_get_request Example Example script
   make_a_patch_request Example Example script
   make_a_post_request Example Example script
   make_a_put_request Example Example script

  Files  /  source  
File Role Description
Builder (2 files)
Dispatcher (3 files)
HeaderLine (9 files)
Option (11 files, 8 directories)
Request (2 files)
Response (1 file)
ResponseBehaviour (3 files)
   FactoryInterface.php Class Class source

  Files  /  source  /  Builder  
File Role Description
   Builder.php Class Class source
   BuilderFactory.php Class Class source

  Files  /  source  /  Dispatcher  
File Role Description
   Dispatcher.php Class Class source
   DispatcherInterface.php Class Class source
   LoggingDispatcher.php Class Class source

  Files  /  source  /  HeaderLine  
File Role Description
   AbstractContentType.php Class Class source
   AbstractHeaderLine.php Class Class source
   AcceptEncoding.php Class Class source
   AcceptLanguage.php Class Class source
   ContentTypeIsUtf8Form.php Class Class source
   ContentTypeIsUtf8Html.php Class Class source
   ContentTypeIsUtf8Json.php Class Class source
   Custom.php Class Class source
   HeaderLineInterface.php Class Class source

  Files  /  source  /  Option  
File Role Description
Authentication (4 files)
Authorization (1 file)
Behaviour (25 files)
Callback (4 files)
Cookie (4 files)
Ftp (10 files)
Security (15 files)
Transfer (28 files)
   AbstractAuthentication.php Class Class source
   AbstractSetOptionArrayValue.php Class Class source
   AbstractSetOptionClosureValue.php Class Class source
   AbstractSetOptionIntValue.php Class Class source
   AbstractSetOptionMixedValue.php Class Class source
   AbstractSetOptionStreamValue.php Class Class source
   AbstractSetOptionStringValue.php Class Class source
   AbstractSetOptionToFalse.php Class Class source
   AbstractSetOptionToTrue.php Class Class source
   OptionInterface.php Class Class source
   SetOption.php Class Class source

  Files  /  source  /  Option  /  Authentication  
File Role Description
   EnableUnrestrictedAuth.php Class Class source
   SetBasicAuthentication.php Class Class source
   SetKeyPassword.php Class Class source
   SetUsernameAndPassword.php Class Class source

  Files  /  source  /  Option  /  Authorization  
File Role Description
   EnableNetrc.php Class Class source

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

  Files  /  source  /  Option  /  Callback  
File Role Description
   SetCallbackForPassWordFunction.php Class Class source
   SetCallbackForProgressFunction.php Class Class source
   SetCallbackForReadFunction.php Class Class source
   SetCallbackForWriteFunction.php Class Class source

  Files  /  source  /  Option  /  Cookie  
File Role Description
   EnableCookieSession.php Class Class source
   SetCookie.php Class Class source
   SetCookieFile.php Class Class source
   SetCookieJar.php Class Class source

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

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

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

  Files  /  source  /  Request  
File Role Description
   Request.php Class Class source
   RequestFactory.php Class Class source

  Files  /  source  /  Response  
File Role Description
   Response.php Class Class source

  Files  /  source  /  ResponseBehaviour  
File Role Description
   ConvertJsonToArrayBehaviour.php Class Class source
   ResponseBehaviourInterface.php Class Class source
   ThrowRuntimeExcept...eLimitBehaviour.php Class Class source

  Files  /  test  
File Role Description
Builder (1 file)
Request (1 file)
Response (1 file)
ResponseBehaviour (2 files)
   AbstractTestCase.php Test Unit test script
   bootstrap.php Test Unit test script

  Files  /  test  /  Builder  
File Role Description
   BuilderTest.php Test Unit test script

  Files  /  test  /  Request  
File Role Description
   RequestTest.php Test Unit test script

  Files  /  test  /  Response  
File Role Description
   ResponseTest.php Test Unit test script

  Files  /  test  /  ResponseBehaviour  
File Role Description
   ConvertJsonToArrayBehaviourTest.php Test Unit test script
   ThrowRuntimeExcept...itBehaviourTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:566
This week:0
All time:5,320
This week:308
 User Ratings  
 
 All time
Utility:93%
Consistency:87%
Documentation:93%
Examples:87%
Tests:87%
Videos:-
Overall:82%
Rank:10
  

For more information send a message to info at phpclasses dot org.