PHP code example of ehough / shortstop

1. Go to this page and download the library: Download ehough/shortstop 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/ */

    

ehough / shortstop example snippets


//build an event dispatcher
$eventDispatcher = new ehough_tickertape_EventDispatcher();  //implements ehough_tickertape_EventDispatcherInterface

//build an HTTP message parser
$httpMessageParser = new ehough_shortstop_impl_exec_DefaultHttpMessageParser();

//build a chain of HTTP transport mechanisms
$chain = new ehough_chaingang_impl_StandardChain();
$chain->addCommand(new ehough_shortstop_impl_exec_command_CurlCommand($httpMessageParser, $eventDispatcher));
$chain->addCommand(new ehough_shortstop_impl_exec_command_ExtCommand($httpMessageParser, $eventDispatcher));
$chain->addCommand(new ehough_shortstop_impl_exec_command_FopenCommand($httpMessageParser, $eventDispatcher));
$chain->addCommand(new ehough_shortstop_impl_exec_command_FsockOpenCommand($httpMessageParser, $eventDispatcher));
$chain->addCommand(new ehough_shortstop_impl_exec_command_StreamsCommand($httpMessageParser, $eventDispatcher));

//build the HTTP client
$client = new ehough_shortstop_impl_DefaultHttpClient($eventDispatcher, $chain);

//build a chain of HTTP content decoders
$contentDecodingChain = new ehough_chaingang_impl_StandardChain();
$contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeGzipDecompressingCommand());
$contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_SimulatedGzipDecompressingCommand());
$contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeDeflateRfc1950DecompressingCommand());
$contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeDeflateRfc1951DecompressingCommand());

//build an HTTP content decoder
$httpContentDecoder = new ehough_shortstop_impl_decoding_content_HttpContentDecodingChain($contentDecodingChain);

//add some HTTP request listeners
$eventDispatcher->addListener(ehough_shortstop_api_Events::REQUEST,
    array(new ehough_shortstop_impl_listeners_request_RequestDefaultHeadersListener($httpContentDecoder), 'onPreRequest'));
$eventDispatcher->addListener(ehough_shortstop_api_Events::REQUEST,
    array(new ehough_shortstop_impl_listeners_request_RequestLoggingListener(), 'onPreRequest'));

//build a chain of HTTP transport decoders
$transferDecodingChain = new ehough_chaingang_impl_StandardChain();
$transferDecodingChain->addCommand(new ehough_shortstop_impl_decoding_transfer_command_ChunkedTransferDecodingCommand());

//build an HTTP transport decoder
$httpTransferDecoder = new ehough_shortstop_impl_decoding_transfer_HttpTransferDecodingChain($transferDecodingChain);

//add some HTTP response listeners
$eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE,
    array(new ehough_shortstop_impl_listeners_response_ResponseDecodingListener($httpTransferDecoder, 'Transfer'), 'onResponse'));
$eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE,
    array(new ehough_shortstop_impl_listeners_response_ResponseDecodingListener($httpContentDecoder, 'Content'), 'onResponse'));
$eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE,
    array(new ehough_shortstop_impl_listeners_response_ResponseLoggingListener(), 'onResponse'));

//build a new HTTP request
$request = new ehough_shortstop_api_HttpRequest('GET', 'https://github.com/');

//execute the request
$response = $client->execute($request);

$status = $response->getStatusCode();    //e.g. 200

$entity = $response->getEntity();        // instance of ehough_shortstop_api_HttpEntity
$type   = $entity->getContentType();     // e.g. text/html
$body   = $entity->getContent();         // the actual body of the response