PHP code example of rsong / phprequest

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

    

rsong / phprequest example snippets


$request = \EasyRequest\Client::request('https://google.com');
$request = \EasyRequest\Client::request('https://google.com', 'GET', $options);
$request = \EasyRequest\Client::request('https://google.com', 'POST', $options);

// then
$request->send();
// or
$request = \EasyRequest\Client::request('https://google.com')->send();

$request = \EasyRequest\Client::request('https://google.com', 'POST', array(
    'protocol_version' => '1.1',
    'method'           => 'GET',
    'header'           => array(),
    'body'             => '',
    'body_as_json'     => false,
    'query'            => array(),
    'form_params'       => array(),
    'multipart'        => array(),
    'default_header'   => true,    // add general headers as browser does
    'upload'           => false,   // wanna upload large files ?
    'cookie_jar'       => null,    // file path or CookieJarInterface

    'bindto'           => null,    // bind to an interface (IPV4 or IPV6), same as CURLOPT_INTERFACE
    'proxy'            => null,
    'proxy_type'       => null,
    'proxy_userpwd'    => null,
    'auth'             => null,

    'timeout'          => 10,      // connect timeout
    'nobody'           => false,   // get header only, you also can use HEAD method
    'follow_redirects' => false,   // boolean or integer (number of max follow redirect)
    'handler'          => null,    // handler for sending request
    'curl'             => array(), // use more curl options ?
));

$request = \EasyRequest\Client::request('https://google.com', 'GET', array(
    'follow_redirects' => 3,
    'query'            => 'a=b&c=d'
));

// or
$request->withOption('follow_redirects', 3);

// or
$request->withOption(array(
    'follow_redirects' => 3,
    'query'            => 'a=b&c=d'
));

$response = \EasyRequest\Client::get('https://google.com', 'GET', array(
    'follow_redirects' => 3,
    'query'            => 'a=b&c=d'
));

$response = \EasyRequest\Client::post('https://google.com', ...);
$response = \EasyRequest\Client::head('https://google.com', ...);
$response = \EasyRequest\Client::patch('https://google.com', ...);
$response = \EasyRequest\Client::delete('https://google.com', ...);

... more 

$request = \EasyRequest\Client::request('https://google.com', 'GET', array(
    'header' => array(
        'User-Agent' => 'Firefox 45',
        'Cookie' => 'cookie1=value1; cookie2=value2;',
        'Example' => array(
            'value 1',
            'value 2'
        )
    ),
));

// you also can use PSR7 as
$request = \EasyRequest\Client::request('https://google.com')
    ->withHeader('User-Agent', 'Firefox 45')
    ->withHeader('Example', array('value1', 'value 2'));

$jar = new \EasyRequest\Cookie\CookieJar;
// or
$jar = new \EasyRequest\Cookie\FileCookieJar($filePath);
// or
$jar = new \EasyRequest\Cookie\SessionCookieJar;

// add cookie from string of multiple cookies
$jar->fromString('cookie1=value1; cookie2=value2');

// add cookie with more information 
$jar->add(Cookie::parse('cookie2=value2; path=/; domain=abc.com')); 

// add cookie from \Psr\Http\Message\ResponseInterface
$jar->fromResponse($response);

// read more at \EasyRequest\Cookie\CookieJarInterface

$request = \EasyRequest\Client::request('https://google.com', 'GET', array(
    'cookie_jar' => $jar
))->send();

var_dump($jar->toArray());
// or
var_dump($request->getOption('cookie_jar')->toArray());

var_dump((string) $jar);

var_dump($jar->getFor($domain, $path));

$options = array(
    'query' => 'a=b&c=d'
);
// or
$options = array(
    'query' => array(
        'a' => 'b',
        'c' => 'd',
        'x' => array(
            'y', 'z'
        ),
        'x2' => array(
            'y2' => 'z2'
        )
    )
);
$request = \EasyRequest\Client::request('https://google.com', 'GET', $options);

    /**
     * Add query string to request.
     *
     * @param  string|array $name      This value may be:
     *                                 - a query string
     *                                 - array of query string
     * @param  null|string  $value
     * @param  bool         $append
     * @param  bool         $recursive
     * @return self
     */
    $request->withQuery($name, $value = null, $append = true, $recursive = false);


$request->withQuery(array(
    'a' => 'b',
    'c' => 'd',
    'x' => array(
        'y', 'z'
    ),
    'x2' => array(
        'y2' => 'z2'
    )
));
// or
$request->withQuery('query=value1&key2=value2');
// or
$request->withQuery('query', 'value1');
// if you want to clear all existing query and add new, just use false for $append
$request->withQuery('query', 'value1', false);

    /**
     * Add form param to request.
     *
     * @param  string|array $name      This value may be:
     *                                 - query string
     *                                 - array of query string
     * @param  null|string  $value
     * @param  bool         $append
     * @param  bool         $recursive
     * @return self
     */
    public function withFormParam($name, $value = null, $append = true, $recursive = false)

$request = \EasyRequest\Client::request('https://google.com', 'GET', array(
    'multipart' => array(
        array(
            'name'     => 'input1',
            'contents' => 'value1'
        ),
        array(
            'name'     => 'input1',
            'contents' => 'value1',
            'filename' => 'custom file name.txt'
            'headers'  => array('Custom-header' => 'value'),
        )
    ),
));

// you also can use
$request
    ->withMultipart('field2', 'value2')
    ->withMultipart('field3', 'value3', 'fieldname3')
    ->withMultipart('field4', 'value4', 'fieldname4', array('Custom-Header' => 'value'))
    ->withMultipart('file5', fopen('/path/to/file'), 'filename1') // to upload file

$request
    ->withFormFile('file1', '/path/to/file1', $optionalFileName = null, $optionalHeaders = array())
    ->withFormFile('file2', '/path/to/file2');

$request->withBody('raw data');

$request->withJson(array(1,2,3));
// or
$request->withJson(json_encode(array(1,2,3)));

$request = \EasyRequest\Client::request('http://domain.com', 'POST', array(
    'proxy'         => '192.168.1.105:8888',
    'proxy_userpwd' => 'user:pass',
    'proxy_type'    => Client::PROXY_SOCKS5, // if not given, it will use this proxy as HTTP_PROXY
));

$request->withProxy('192.168.1.105:8888', 'user:pass', Client::PROXY_SOCKS5);
$request->withProxy('192.168.1.105:8888', null, Client::PROXY_SOCKS4);
$request->withProxy('192.168.1.105:8888', null, Client::PROXY_HTTP);

$request->withSocks4Proxy('192.168.1.105:8888'); 
$request->withSocks5Proxy('192.168.1.105:8888', 'user:pass'); 
$request->withHttpProxy('192.168.1.105:8888', 'user:pass'); 

$request = \EasyRequest\Client::request('http://domain.com', 'POST', array(
    'auth' => 'user:pass',
));

$request = \EasyRequest\Client::request('http://domain.com', 'POST', array(
    'bindto' => '123.123.123.123', // same as CURLOPT_INTERFACE option
));

$request = \EasyRequest\Client::request('http://domain.com', 'POST');
$response = $request->send();

// Returns \Psr\Http\Message\RequestInterface
var_dump($request->getRequest());

// Returns \Psr\Http\Message\ResponseInterface
// Or null if request is not sent or failure
var_dump($request->getResponse());

var_dump($response);

$request->getCurrentUri();

$response = $request->getResponse();

// you can use PSR7 here
$response->getHeaders();
$response->getHeader('Set-Cookie');
$response->getHeaderLine('Location');

$response->getProtocolVersion();

echo (string) $response->getBody();

var_dump($request->getRequests());

var_dump($request->getResponses());