Class goText_Browser

Description

A simple HTTP client class specifically made to serve goText, Information Framework and Simple Mobile Services needs.

It is a sort of wrapper around cURL functions with support for multiple requests and cookie/session persistence, with the use of PHP 5.

To use this class for a GET request you need only 3 simple steps:

  1. Include class sources:
    1. include('goText'.DIRECTORY_SEPARATOR.'Browser.php');
  2. Create an instance of the class:
    1. $browser new goText_Browser(TRUETRUE);
  3. Make your GET request an retrieve the response page:
    1. $page $browser->browseGetUrl($address,true)

That's all! You don't need to care of redirects and cookie handling, the class keep track of everything! You can POST data, that is make a post request, with the same ease:

  1. $page $browser->browsePostUrl($address,$poststring,true)

This is all you normally need. The class still provides additional methods to do additional tweaking just in case you need it.

  • author: Fulvio 'Zydio' Sciarretta <zydio@users.sf.net>
  • version: 1.0
  • copyright: Zydio (c) 2007-2009
  • access: public
  • license: GNU

Located in /Browser.php (line 81)


	
			
Class Constant Summary
 HTTP_USER_AGENT_FIREFOX = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4"
 HTTP_USER_AGENT_MSIE6 = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
 MAX_REDIRS = 10
Method Summary
goText_Browser __construct ([boolean $cookiePersist = false], [boolean $sendReferers = false])
void __destruct ()
string|boolean browseGetUrl (string $url, [bool $follow_redirects = true])
string|boolean browsePostUrl (string $url, string|array $query, [boolean $follow_redirects = true])
string browsePostUrlWithFile (string $url, array $postArray, string $fileFormFieldName, string $localFilePath, [boolean $follow_redirects = true])
void getCurrentUrl ()
string|boolean getLocationRedirect ()
array getMetaInfos ()
boolean restoreCookies ()
void restoreReferer ()
boolean saveCookies ()
void saveReferer ()
void setBrowserProxy (string $address, string $user, string $pass, int $type, [string $domain = null])
void setHttpVersion10 ([boolean $set_1_0 = true])
void setReferer (string $referer)
void setRequestHeaders ( $headers)
void setTimeout (int $seconds)
void setUserAgent (string $new_userAgent)
void setVerboseCURLOutput (boolean $verbose_enabled)
Methods
Constructor __construct (line 225)

Used to create a new instance of the Browser class.

Requesting cookie persistance (= true) still require manual work from the caller, that means calling saveCookies() and restoreCookies().
(Anyway all session persistance work is done in browser class, the caller doens't have to store session information for the browser.)
Cookie persistance value doesn't affect cookie handling: cooke are always automatically retrieved and sent back in following requests.

Referer must be enabled on some website to browse them without errors, but usually is not required.

  • access: protected
goText_Browser __construct ([boolean $cookiePersist = false], [boolean $sendReferers = false])
  • boolean $cookiePersist: Cookie persistance enabled or disabled (this means only that cookies can be saved or not for later use)
  • boolean $sendReferers: Referer getting and sending enabled or disabled
Destructor __destruct (line 247)

Destructor

Releases cURL resource

  • access: protected
void __destruct ()
browseGetUrl (line 445)

Make a HTTP GET request to the URL, returning the response page.

  • return: The retrieved page content (HTML code) or FALSE if errors occured
  • access: public
string|boolean browseGetUrl (string $url, [bool $follow_redirects = true])
  • string $url: URL to retrieve
  • bool $follow_redirects: Whether redirects should be automatically followed
browsePostUrl (line 478)

Make a HTTP POST request to the URL, returning the response page.

  • return: The retrieved page content (HTML code) or FALSE if errors occured
  • access: public
string|boolean browsePostUrl (string $url, string|array $query, [boolean $follow_redirects = true])
  • string $url: URL
  • string|array $query: The (already urlencoded) query to POST
  • boolean $follow_redirects: Whether redirects should be automatically followed
browsePostUrlWithFile (line 517)

Make a HTTP POST request along with a file (in "multipart/form-data" way) to the URL, returning the response page.

  • return: The retrieved page content (HTML code)
  • access: public
string browsePostUrlWithFile (string $url, array $postArray, string $fileFormFieldName, string $localFilePath, [boolean $follow_redirects = true])
  • string $url: URL
  • array $postArray: The (already urlencoded) query to POST
  • string $fileFormFieldName: The field name of form's file upload field
  • string $localFilePath: The full path of file to upload on LOCAL filesystem
  • boolean $follow_redirects: Whether redirects should be automatically followed
enableDebugWithFullEcho (line 698)

Enables browser calss own debug output. This is for development environment only!

When enabled the browseGetUrl() browsePostUrl() return the headers along with page content, and some debug strings are also printed here and there.

  • access: public
void enableDebugWithFullEcho ()
getCurrentUrl (line 809)

Returns current url

The value returned from this method may be different from last requested url (in a get/post), because of redirect following

  • global: string $currentUrl: Used to retrieve correctly this value also on buggy environments in callbacks (like _readHeader())
  • access: public
void getCurrentUrl ()
getLocationRedirect (line 826)

If latest GET/POST operation was requested to not follow redirects, you can retrieve the redirect url (if any) with this method, to handle it.

string|boolean getLocationRedirect ()
getMetaInfos (line 958)

Returns current response meta infos and latest response headers.

This is not useful until you need to check something in response header content.

  • return: Array of response meta infos and eventually headers
  • access: public
array getMetaInfos ()
restoreCookies (line 772)

Restores cookie previously saved in session with saveCookies().

  • return: True if restore was done, false if not (cookie persistance disabled or cookie not found)
  • see: goText_Browser::__construct()
  • access: public
boolean restoreCookies ()
restoreReferer (line 758)

Restoring a referer saved in a previous session.

Restores a referer previously saved with saveReferer()

  • access: public
void restoreReferer ()
saveCookies (line 726)

Saves all cookies for this browsing session in.. $_SESSION .

Once saved the cookie can be later restored calling restoreCookies(), without anything else to do.

boolean saveCookies ()
saveReferer (line 746)

Saves the current, last, referer in $_SESSION.

Once saved the referer can be later restored calling restoreReferer(), without anything else to do.

  • access: public
void saveReferer ()
setBrowserProxy (line 666)

Sets authentication proxy for outgoing connections

I don't think it can be useful in production environments, but can be useful while developing locally.

  • access: public
void setBrowserProxy (string $address, string $user, string $pass, int $type, [string $domain = null])
  • string $address: Proxy address
  • string $user: Proxy user for authentication
  • string $pass: Proxy password for authentication
  • int $type: May be only CURLAUTH_BASIC or CURLAUTH_NTLM
  • string $domain: Proxy domain to be filled if authentication needs domain
setHttpVersion10 (line 851)

Sets HTTP version to 1.0 (default goText_Browser setting is HTTP 1.1)

  • access: public
void setHttpVersion10 ([boolean $set_1_0 = true])
  • boolean $set_1_0: true to set HTTP version to 1.0 , false to se to 1.1
setReferer (line 866)

Can be used to set the referer to an arbitrary URL

  • access: public
void setReferer (string $referer)
  • string $referer: The new referer URL
setRequestHeaders (line 529)

Used to set a custom HTTP Request header.

  • access: public
void setRequestHeaders ( $headers)
  • $headers
setTimeout (line 647)

Sets timeout for all get/post requests after this call

  • access: public
void setTimeout (int $seconds)
  • int $seconds: time in seconds to wait before timing out
setUserAgent (line 840)

Changes the User-Agent string sent by the browser

  • access: public
void setUserAgent (string $new_userAgent)
  • string $new_userAgent: User-Agent string of choice
setVerboseCURLOutput (line 630)

Enable verbose output (of underlying cURL)

  • access: public
void setVerboseCURLOutput (boolean $verbose_enabled)
  • boolean $verbose_enabled: true to enable cURL verbose output
Class Constants
DIRECT_CONNECTION = -100 (line 201)

Constant to tell we are not using proxy.

Used in some checks around the code, has no meaning outside.

HTTP_USER_AGENT_FIREFOX = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4" (line 191)

User Agent string for Firefox 3.x

HTTP_USER_AGENT_MSIE6 = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" (line 195)

User Agent string for Internet Explorer 6

MAX_REDIRS = 10 (line 205)

Maximum number of redirects to be followed (to avoid redirect loops)

Documentation generated on Tue, 10 Feb 2009 16:58:07 +0100 by phpDocumentor 1.4.0a2