2017 © Pedro Peláez
 

package phprequest

PHP Request

image

masterklavi/phprequest

PHP Request

  • Wednesday, June 7, 2017
  • by masterklavi
  • Repository
  • 1 Watchers
  • 0 Stars
  • 30 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

PHP Request

It includes some functions to easy requesting and parsing data., (*1)

Examples

use \phprequest\Request;

print Request::request('https://api.ipify.org/'); // prints your IP address received from https://api.ipify.org/
print PHP_EOL;

print Request::get('https://api.ipify.org?format=json', ['filter' => 'json'])->ip . PHP_EOL;
print Request::get('https://api.ipify.org?format=json', ['filter' => 'json_assoc'])['ip'] . PHP_EOL;

// Request::post('http://example.com', ['data' => ['name' => 'John']]);

// Request::multi(['http://example.com', 'http://example.com/1', 'http://example.com/2'], ['concurrency' => 2]);
// Request::multiPost(['http://example.com', ['http://example.com/1', ['filter' => 'json']], 'http://example.com/2'], ['concurrency' => 2]);

Requirements

  • PHP version 5.4.0 or higher
  • PHP extension ext-curl enabled

Installation

Using Composer

Get the package:, (*2)

$ composer require masterklavi/phprequest

Include vendor/autoload.php:, (*3)

include 'vendor/autoload.php';
use \phprequest\Request;

$data = Request::get('http://www.cbr-xml-daily.ru/daily_json.js', ['filter' => 'json']);
print 'USD: ' . $data->Valute->USD->Value, PHP_EOL;

Manual Installation

Clone git repository:, (*4)

$ git clone https://github.com/masterklavi/phprequest.git

or download the package at https://github.com/masterklavi/phprequest/archive/master.zip, (*5)

Include autoload.php:, (*6)

<?php
include 'autoload.php';
use \phprequest\Request;

$data = Request::get('http://www.cbr-xml-daily.ru/daily_json.js', ['filter' => 'json']);
print 'USD: ' . $data->Valute->USD->Value, PHP_EOL;

Methods

Request::request

Requests the given URL., (*7)

Syntax

mixed Request::request(string $url, array $options = [])
Parameters

$url – URL address
$options – Array of request options, (*8)

Return Values

Returns a response (body) as string by default. Returns a mixed value when the filter option's set. Returns false on failure., (*9)

Request::get

Alias of Request::request(), (*10)

Request::post

Equivalent of Request::request($url, ['method' => 'POST']), (*11)

Request::multi

Requests the given URLs (parallel requests using curl multi)., (*12)

Syntax

mixed Request::multi(string $urls, array $options = [])
Parameters

$url – Array of URL address strings or arrays of URL addresses and their options (e.g. [ 'http://a.ru', ['http://b.ru', ['filter' => 'json']] ])
$options – Array of request options, (*13)

Return Values

Returns an array of results for the given urls. Result may contain: - a response (body) as string by default - a mixed value when the filter option's set - false on failure, (*14)

Request::multiGet

Alias of Request::multi(), (*15)

Request::multiPost

Equivalent of Request::multi($urls, ['method' => 'POST']), (*16)

Request Options

List of curl options:, (*17)

Name Type Default Description
method string GET The method of the HTTP request (GET, POST, HEAD, PUT, DELETE) (see CURLOPT_POST, CURLOPT_CUSTOMREQUEST)
data string, array Querystring for GET, HEAD and DELETE requests, or request body for others (see CURLOPT_POSTFIELDS)
follow boolean false Follow HTTP redirections (see CURLOPT_FOLLOWLOCATION)
encoding string The contents of the "Accept-Encoding: " header (see CURLOPT_ENCODING)
timeout integer 300 The timeout of one request (see CURLOPT_TIMEOUT)
session string Path to cookie-based session file (enables session between different calls) (see CURLOPT_COOKIEJAR)
cookie string The contents of the "Cookie: " header (see CURLOPT_COOKIE)
headers array An array of HTTP headers (see CURLOPT_HTTPHEADER)
referer string The contents of the "Referer: " header (see CURLOPT_REFERER)
useragent string The contents of the "User-Agent: " header (see CURLOPT_USERAGENT)
proxy string Proxy string like IP:PORT or username:password@IP:PORT (see CURLOPT_PROXY, CURLOPT_PROXYUSERPWD)
interface string Used interface (IP or host) (see CURLOPT_INTERFACE)
curl array An array of custom curl options

List of special options:, (*18)

Name Type Default Description
allowed_codes array [200] Allowed HTTP codes
allow_empty boolean false Allows empty body of the HTTP response
filter string, callable Filters body (usually to reduce memory usage). See Available filters
charset string The charset of requested content (the result will contain 'utf8')
attempts integer 5 Number of request attempts
concurrency integer 10 Concurrency of requests in Request::multi()

Filters

Plain filters:, (*19)

Request::get($url, ['filter' => 'json']);
  • json - interprets the body as json and returns an object
  • json_assoc - interprets the body as json and returns an associative array
  • xml - interprets the body as xml and returns SimpleXML objects
  • plain - returns full response (with headers) as plain text
  • headers - returns a set of the headers
  • headers_body - returns a set of the headers and the body as plain text

Complex filters:, (*20)

Request::get($url, ['filter' => ['filter_name', $option]])
  • cut - cuts out a new substring using begin and end substrings (tags)
    Syntax:, (*21)

    Request::get($url, ['filter' => ['cut', $options]])
    

    where $options is key-value array that may contains:, (*22)

    • 'begin' => 'some string' - substring as beginning of the cut
    • 'end' => 'some string' - substring as ending of the cut
    • 'case_sensivity' => true - use begin and end as case sensivity substrings
    • 'mbstring' => true - use mb_* functions on cutting
  • regex - returns an array of the results matched the regular expression (uses preg_match)
    Syntax:, (*23)

    Request::get($url, ['filter' => ['cut', '#reg.exp. pattern#']])
    

    Returns null if the pattern doesn't match the body, or false if an error occured, (*24)

  • regex_one - returns the first matched text (uses preg_match), (*25)

  • regex_all - returns an array of arrays of the results of search used the regular expression (uses preg_match_all)
  • regex_set - returns an array of arrays of the results of search used the regular expression (uses preg_match_all with the set order)
  • regex_col - returns an array of the results of the first values matched the regular expression (uses preg_match_all)

The Versions

07/06 2017

dev-master

9999999-dev

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

07/06 2017

v0.3.6

0.3.6.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

20/04 2017

v0.3.5

0.3.5.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

20/04 2017

v0.3.4

0.3.4.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

20/04 2017

v0.3.3

0.3.3.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

17/04 2017

v0.3.2

0.3.2.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

05/04 2017

v0.3.1

0.3.1.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

07/02 2017

v0.3.0

0.3.0.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

02/02 2017

v0.2.0

0.2.0.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *

 

02/02 2017

v0.1.0

0.1.0.0

PHP Request

  Sources   Download

The Requires

  • php >=5.4.0
  • ext-curl *
  • lib-curl *