PHP code example of jichangfeng / request-url

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

    

jichangfeng / request-url example snippets


// Complex request
$result = RequestUrl::getInstance()
        ->enableCookie('test.tmp')
        ->enableGzip(true)
        ->setUserAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36')
        ->setReferer('https://example.com')
        ->setTimeout(10)
        ->setConnectTimeout(5)
        ->setHeaders(array('Accept' => 'application/json'))
        ->setParams(array('name' => 'admin', 'password' => '123456'))
        ->setProxy('127.0.0.1', 1080)
        ->post('https://www.example.com/user/login');

// Simple request
$result = RequestUrl::getInstance()->get('https://www.example.com');
$result = RequestUrl::getInstance()->setParams(array('key' => 'value'))->post('https://www.example.com');

// application/x-www-data-urlencoded
//   application/x-www-form-urlencoded is the default form submission format.
//   When using cURL to send a request, if the Content-Type is not set, this format will be used by default.
$result = RequestUrl::getInstance()
        ->setParams(array('key' => 'value'))
        ->post('https://www.example.com');
//   But if this header is set manually, cURL will send the request body as a plain text string instead of form-encoded.
//   At this time, you must use the http_build_query() function to manually build form data.
$result = RequestUrl::getInstance()
        ->setHeaders(array('Content-Type' => 'application/x-www-data-urlencoded'))
        ->setParams(http_build_query(array('key' => 'value')))
        ->post('https://www.example.com');

// multipart/form-data
$result = RequestUrl::getInstance()
        ->setHeaders(array('Content-Type' => 'multipart/form-data'))
        ->setParams(array('avatar' => new CURLFile('/tmp/avatar.png'), 'nickname' => 'coco'))
        ->post('https://www.example.com');

// application/json
$result = RequestUrl::getInstance()
        ->setHeaders(array('Content-Type' => 'application/json'))
        ->setParams(json_encode(array('key' => 'value')))
        ->post('https://www.example.com');

var_dump($result['body']);
// string '[...]'... (length=1270)

var_export($result['header']);
/*
'HTTP/2 200 
cache-control: max-age=604800
content-type: text/html; charset=UTF-8
date: Tue, 13 Aug 2019 10:26:21 GMT
etag: "1541025663+ident"
expires: Tue, 20 Aug 2019 10:26:21 GMT
last-modified: Fri, 09 Aug 2013 23:54:35 GMT
server: ECS (dcb/7F38)
vary: Accept-Encoding
x-cache: HIT
content-length: 1270

'
*/

$headers = RequestUrl::getInstance()->parseHeader($result['header']);
var_export($headers);
/*
array (
  'cache-control' => 'max-age=604800',
  'content-type' => 'text/html; charset=UTF-8',
  'date' => 'Tue, 13 Aug 2019 10:26:21 GMT',
  'etag' => '"1541025663+ident"',
  'expires' => 'Tue, 20 Aug 2019 10:26:21 GMT',
  'last-modified' => 'Fri, 09 Aug 2013 23:54:35 GMT',
  'server' => 'ECS (dcb/7F38)',
  'vary' => 'Accept-Encoding',
  'x-cache' => 'HIT',
  'content-length' => '1270',
)
*/

var_export($result['info']);
/*
array (
  'url' => 'https://www.example.com',
  'httpCode' => 200,
  'contentType' => 'text/html; charset=UTF-8',
  'contentTypeDownload' => 1270.0,
  'sizeDownload' => 1270.0,
  'elapsedTime' => 1.7826,
  'errno' => 0,
  'error' => '',
  'proxy' => false,
)
*/