PHP code example of net_bazzline / php_component_curl

1. Go to this page and download the library: Download net_bazzline/php_component_curl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

net_bazzline / php_component_curl example snippets


//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;

//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;

//begin of runtime environments
$factory        = new BuilderFactory();
$builder        = $factory->create();
$data           = array(
    'there'     => 'is',
    'no'        => 'foo',
    'without'   => 'a bar'
);
$password       = '<super secret password>';
$username       = '[email protected]';
$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);

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

composer