Back to the module list

HTTP client

Get pages from site, manage the cookies between the pages, allow to follow the redirects. The cookies can be stored in an external file compatible with Netscape's cookies.txt. It allow to send forms fields or files.

Example 1 : Get a page

$httpclient = new Domframework\Httpclient ();
$httpclient->useragent ("Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0")
$content = $httpclient->getPage ("");

Example 2 : Request a JSON page

$httpclient = new Domframework\Httpclient ();
$httpclient->headerAdd ("Content-Type", "application/json")
           ->url ("")
           ->method ("POST");
$httpclient->connect ();
$page3 = $httpclient->getContent ();

Example 3 : Send form data

$page6 = $httpclient->method ("POST")
                    ->formData (['taskId' => 'aUniversMesComptes'])
                    ->url ($url)
                    ->connect ()
                    ->getContent ();

The class definition

Class Domframework\Httpclient

Namespace Domframework


 This programe allow to get a HTTP page from a site, and examine the content.
 It will store the Cookies, allow to do the redirects, follow links and
 get form / input and send the values.


No property available


public function __construct ()
 The constructor

public function accept ( $accept=null)
 Get/Set the accept type of page wanted by the client
 @param string|null $accept The accept types with weight

public function acceptEncoding ( $acceptEncoding=null)
 Get/Set the accept Encoding wanted by the client
 @param string|null $acceptEncoding The encoding requested

public function acceptLanguage ( $acceptLanguage=null)
 Get/Set the accept Language wanted by the client
 @param string|null $acceptLanguage The languages with weight

public function authBasic ( $login, $password)
 Set the authentication in Basic type
 @param string $login The login to use
 @param string $password The password to use

public function authentication ( $auth=null)
 Get/Set authentication
 To remove authentication, pass "" to $auth arg
 @param string|null $auth The authentication string to send
 @return value or $this

public function baseURL ()
 Return the base URL of the site
 @return the URL

public function connect ( $ssloptions=null)
 Init the connection to URL
 Will fill the headersReceived, cookies and port properties.
 @param array|null $ssloptions The SSL options (stream_context_set_option)
 @return $this

public function cookieAdd ( $domain, $cookie)
 Add a cookie in the store
 If the cookie already exists, the old one is replaced by the new value
 @param string $domain The domain to use
 @param string $cookie The cookie content to store

public function cookieToSend ( $url)
 Check if some stored cookies must be send to the server, because they are
 in the same domain.
 @param string $url The URL requested
 @return array the cookies to add to the headers send to the server

public function cookies ( $cookies=null)
 Set / Get the cookies stored
 @param array|null $cookies Set / Get the cookies

public function cookiesSession ( $cookiesSession=null)
 Get / Set the Store of session cookies when analyzing the answer of the
 @param boolean|null $cookiesSession Allow to store the session cookies

public function debug ( $debug=null)
 Set / Get the debug mode
 0: Nothing is displayed, 1: Only URL are displayed,
 2: headers only send and received, 3: all the content, but without sent
 files, 4: all the content
 @param boolean|null $debug The debug value to set or get

public function disconnect ()
 Disconnect the connection

public function formData ( $formData=null)
 Set / Get the form Data
 Will be of type array ("field" => "value")
 If value is like "@/tmp/file", use the /tmp/file as content
 @param array|null $formData The data to send to the server

public function getContent ()
 Get the content from the server and put it in memory

public function getInfo ()
 Return the TCP infos of the connection

public function getMeta ()
 Get meta data, like the timeout state, the crypto protocol and ciphers...

public function getPage ( $url, $ssloptions=null)
 Get the page
 Will fill the headersReceived, cookies and port properties.
 This will fill all the RAM if the page is too big. For big files, use
    $httpclient->url ($url)
               ->connect () ;
    while ($content = $httpclient->read ()) {}
    $httpclient->disconnect ();
 If no maxsize limit is set, limit the download to 8G
 @param string $url The URL to get
 @param array|null $ssloptions The SSL options (stream_context_set_option)
 @return the page body

public function headerAdd ( $header, $value)
 Add a new header to be sent to the server
 @param string $header The header to add/update
 @param string $value The value to save

public function headersReceived ()
 Get the headersReceived after the page was get

public function headersReset ()
 Set the headers to initial value

public function headersSent ()
 Get the headers sent to the server after the page was get

public function httpCode ()
 Get the HTTP Return code from connection

public function log ( $priority, $message)
 Display the log message
 @param integer $priority The minimal priority to display the message
 @param mixed $message The message to display

public function maxsize ( $maxsize=null)
 Set / Get the maximum maxsize allowed
 @param integer|null $maxsize The maxsize in bytes

public function method ( $method=null)
 Set / Get the method
 @param string|null $method Set / Get the method

public function port ()
 Get the port used for connection

public function rawData ( $rawData=null)
 Set / Get the content Data
 The data is in raw format and will not be modified.
 Overwrite the eventually previous form data and rawData
 @param string|null $rawData The data to use

public function read ( $maxsize=4096)
 Read max MAXSIZE bytes
 Return "" if all the file is received
 3 methods are supported : Chunked mode, Content-Length defined and all
 until the connection will be closed by the server
 @param integer $maxsize The maxsize to get in this read

public function redirectCount ( $redirectCount=null)
 Get / Set the actual number of redirect
 @param integer|null $redirectCount The actual number of redirect

public function redirectMaxCount ( $redirectMaxCount=null)
 Get / Set the maximum number of redirect to follow before aborting
 @param integer|null $redirectMaxCount The maximum number of redirect
 before exception

public function referer ( $referer=null)
 Get/Set the referer page
 @param string|null $referer The new referer that will be used on next

public function ssloptions ( $ssloptions=null)
 Get/Set the ssl options
 @param array|null $ssloptions The SSL Options to use
 @return $this

public function timeout ( $timeout=null)
 Get / Set the timeout in second before expiring the connection
 30s by default
 @param integer|null $timeout The timeout value

public function url ( $url=null)
 Set / Get the url
 @param string|null $url Set / Get the url

public function useragent ( $useragent=null)
 Get / Set the useragent sent to the server. If it is empty, it will not be
 @param string|null $useragent The user agent to use