GithubHelp home page GithubHelp logo

http-1's Introduction

HTTP Package

HTTP Request/Response Package for PHP 5.4+

Quick Start

Use example.php to test:

// load HTTP package files
require_once './lib/Http/Request.php';
require_once './lib/Http/Response.php';

try
{
	// create new HTTP request object
	$req = new \Http\Request('htp://www.example.com/');

	// set request params example:
	$req->param('var1', 'value_1');
	$req->param('var2', 'value_2');

	// set HTTP Response object with HTTP GET request
	$res = $req->get();

	if($res->is_success) // 200/OK
	{
		echo 'Response code: ' . $res->getResponseCode() . ', '; // ex: 200
		echo 'Response headers: ' . print_r($res->getHeaders(), true) . ', '; // array of headers
		echo 'Total seconds taken: ' . $res->getElapsedTime() . ', '; // total seconds for request
		echo 'Response string: ' . $res->getResponseString();
	}
	else if($res->is_error) // print error
	{
		echo 'Error: ' . $res->error;
	}
}
catch(\Exception $ex)
{
	echo 'Exception: ' . $ex->getMessage();
}

For a HTTP POST request example simply change the call method to:

...
// set HTTP Response object with HTTP POST request
$res = $req->post();
...

For a HTTP HEAD request example simply change the call method to:

...
// set HTTP Response object with HTTP HEAD request
$res = $req->head();
...

Other Response Class Methods

The HTTP Response class has several other helpful methods

getHeadersObject():\stdClass

This method returns the headers in an objects so header data can be accessed like:

$headers = $res->getHeadersObject();
echo 'Server type: ' . $headers->server;

getRequestUrl():string

Gets the original request URL with query string (if relates to request method type)

match($str_or_pattern):int

This method can be used to check the count of any value or regex pattern in the response string, for example:

echo 'Match count: ' . $res->match('keyword'); // total count
// or use regex pattern like /x/
echo 'Match count: ' . $res->match('/keyword/i'); // total count

extract(string $regex_pattern, callable $array_map_callback):array

This method is used to extract data from the response string based on regex pattern, for example:

// get array of all strings that have 'keyword' and end with '.'
$extracted_data = $res->extract('/keyword[^\.]+\./i');

// and can use callback to modify array strings, like removing all HTML tags from strings:
$extracted_clean = $res->extract('/keyword[^\.]+\./i', function($v) { return strip_tags($v); });

HTTP Options

HTTP Request properties can be used to change HTTP request options

Referer

A custom referer can be set using:

$req->referer = 'http://www.example.com';

Request Timeout

The request timeout (in seconds) can be modified using:

$req->timeout_seconds = 10;

Use cURL Library

By default the HTTP package uses the file_get_contents() function for HTTP requests, this can be change to use the cURL library using the option:

$req->use_curl = true;

User Agent

A custom user agent can be set using:

$req->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8';

http-1's People

Contributors

shayanderson avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.