PHP code example of comodojo / httprequest

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

    

comodojo / httprequest example snippets


    try {

	    // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");

        // or:
        // $http = new \Comodojo\Httprequest\Httprequest();
        // $http->setHost("www.example.com");

        // get remote data
        $result = $http->get();

	} catch (\Comodojo\Exception\HttpException $he) {

		/* handle http specific exception */

	} catch (\Exception $e) {

		/* handle generic exception */

	}

	

    $data = array('foo'=>'bar', 'baz'=>'boom');

    try {

	    // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");

        // get remote data
        $result = $http->setHttpMethod("POST")->send($data);

	} catch (\Comodojo\Exception\HttpException $he) {

		/* handle http specific exception */

	} catch (\Exception $e) {

		/* handle generic exception */

	}

	

    $http->setPort(8080);

    

    $http->setTimeout(10);

    

    $http->setUserAgent("My-Custom-User-Agent");

    

    $http->setHttpVersion("1.1");

    

    $http->setContentType("multipart/form-data");

    

    $http->setHeader("My-Header","foo");

    

    // NTLM
    $http->setAuth("NTLM", "myusername", "mypassword");

    // BASIC
    $http->setAuth("BASIC", "myusername", "mypassword");

    

    // No authentication
    $http->setProxy(proxy.example.org);

    // Authentication
    $http->setProxy(proxy.example.org, "myusername", "mypassword");

    

    // Set the stream to ignore errors
    $http->setIgnoreErrors(true);

    

    // After a request...

    $headers = $http->getReceivedHeaders();

    

    // After a request...

    $code = $http->getHttpStatusCode();

    

try {

    // create an instance of Httprequest
    $http = new \Comodojo\Httprequest\Httprequest();

    // first request
    $first_data = $http->setHost("www.example.com")->get();

    // channel reset
    $http->reset();

    // second request
    $second_data = $http->setHost("www.example2.com")->setHttpMethod("POST")->send(array("my"=>"data"));

} catch (\Comodojo\Exception\HttpException $he) {

	/* handle http specific exception */

} catch (\Exception $e) {

	/* handle generic exception */

}