Smart.Framework Logo

final class \SmartHttpClient
{ } ->

Class: SmartHttpClient - provides a HTTP / HTTPS Client (browser).
To work with TLS / SSL (requires the PHP OpenSSL Module).
Implemented Methods: Standard (GET / POST / HEAD) ; Extended (PUT / PATCH / DELETE / OPTIONS / MKCOL / MOVE / COPY / PROPFIND).
The Standard Methods will prefere HTTP 1.0 (which provide a faster access)
The Extended Methods will prefere HTTP 1.1 instead of 1.0 because of some additional headers that may validate for error checks ...


class Methods

public function __construct ( ENUM $http_protocol = "1.0" ) {} @ 
@param: {ENUM} $http_protocol: The HTTP protocol version to use ; can be set to 1.0 or 1.1 ; default is 1.0 (HTTP 1.0) which is fastest for single requests, especially in simple situations like GET or POST ; can be set to 1.1 (HTTP 1.1) which may be required in special situations for more complex requests ; HTTP 1.1 mostly will serve transfer content chunked on GET/POST so for GET/POST is better to sue 1.0 if supported by server
Class constructor
public function set_ssl_tls_ca_file ( $cafile ) {} -> 
This is only for special cases and can be used before calling the browse_url() for the cases when the client requires a custom SSL Certificate to be set
Set a SSL/TLS Certificate Authority File ; by default will use the SMART_FRAMEWORK_SSL_CA_FILE (if defined, and non-empty)
public function browse_url ( $url, $method = "GET", $ssl_version = "", $user = "", $pwd = "" ) {} -> 

class Properties

public $useragent = 'NetSurf/3.10 (Smart.Framework {version}; {OS} {Release} {Arch}; PHP/{php-ver})' ;  -> STRING
User agent (if used as robot, it must have the robot in the name to avoid start un-necessary sessions)
public $connect_timeout = 30 ;  -> INTEGER+
Connect timeout in seconds
public $rawheaders = [] ;  -> ARRAY
Array of RawHeaders (to send)
public $cookies = [] ;  -> ARRAY
Array of Cookies (to send)
public $posttype = '' ;  -> STRING
Pre-Built Post Type (used to set the Content-Type header) ; Example: 'application/json'
This should be used just with $poststring to set a different content type than default which is 'application/x-www-form-urlencoded'
public $poststring = '' ;  -> STRING
Pre-Built Post String (as alternative to PostVars) ; must not contain unencoded \r\n ; must use the RFC 3986 standard.
If $poststring is used the $postvars and $postfiles are ignored
public $postvars = [] ;  -> ARRAY
Array of PostVars (to send)
public $postfiles = [] ;  -> ARRAY
Array of PostFiles (to send) ; This can be used only in combination with $postvars ; Example [ 'filename' => 'file.txt', 'content' => 'the contents go here' ]
public $putbodymode = 'string' ;  -> ENUM
PUT Request Mode (used for $putbodyres)
Can have one of the values: 'string' or 'file'
public $putbodyres = '' ;  -> MIXED
PUT Request (to send)
If $putbodymode is set to 'string', this string will be sent as put
If $putbodymode is set to 'file', the content of this file will be sent as put ; this must be a valid relative path to a file (ex: tmp/file-to-put.txt)
public $skipcontentif401 = true ;  -> BOOLEAN
Return no Content (empty response body) if Not Auth (401)
If the response HTTP Status will return 401, if this is set to TRUE, will return empty content body ; else will return the HTML response body that server response set for 401 (irrelevant but in some cases may be important to parse)
public $skip100continue = false ;  -> BOOLEAN
If set to TRUE will disable expect-100-continue and will skip parsing 100-continue header from server
Applies just for HTTP 1.1 and
This is a fix for those servers that does not comply with situations where 100-continue is required on HTTP 1.1
public $debug = false ;  -> BOOLEAN
Enable or Disable Debug for this class and the HTTP Request
If set to TRUE will enable debug

class Constants


Sample code: PHP

<?php

// Sample GET
 
$browser = new SmartHttpClient(); // HTTP 1.0
 
$browser->connect_timeout 20;
 
print_r(
         
$browser->browse_url('https://some-website.ext:443/some-path/''GET')
 );

 
// Sample POST, with optional Files and Cookies ; If Files, will send multipart form data
 
$browser = new SmartHttpClient('1.1'); // HTTP 1.1
 
$browser->postvars = [
         
'var1' => 'val1',
         
'var2' => 'val2'
 
];
 
$browser->postfiles = [ // optional
         
'my_file' => [
             
'filename' => 'sample.txt',
             
'content'  => 'this is the content of the file'
         
],
         
'my_other_file' => [
             
'filename' => 'sample.xml',
             
'content'  => '<xml>test</xml>'
         
]
 ];
 
$browser->cookies = [ // optional
         
'sessionID' => '12345'
 
];
 
print_r(
         
$browser->browse_url('https://some-website.ext:443/some-path/''POST')
 );

 
// Sample PUT
 
$browser = new SmartHttpClient('1.1'); // HTTP 1.1
 
$browser->connect_timeout 20;
 
$browser->putbodyres 'tmp/test-file.txt'// using a file
 
$browser->putbodymode 'file';
 
//$browser->putbodyres = '123'; // alternate can use a string
 //$browser->putbodymode = 'string';
 
print_r(
         
$browser->browse_url('https://some-website.ext:443/some-path/~/some-file.txt''PUT''''admin''pass')
 );

// #end php code


documentation generated on: 2023-10-19 23:15:45 +0000


Smart.Framework © 2009-2023 unix-world.org