ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
HTTP_Request Class Reference
+ Collaboration diagram for HTTP_Request:

Public Member Functions

 HTTP_Request ($url='', $params=array())
 #- More...
 
 _generateHostHeader ()
 Generates a Host header for HTTP/1.1 requests. More...
 
 reset ($url, $params=array())
 Resets the object to its initial state (DEPRECATED). More...
 
 setURL ($url)
 Sets the URL to be requested. More...
 
 getUrl ()
 Returns the current request URL. More...
 
 setProxy ($host, $port=8080, $user=null, $pass=null)
 Sets a proxy to be used. More...
 
 setBasicAuth ($user, $pass)
 Sets basic authentication parameters. More...
 
 setMethod ($method)
 Sets the method to be used, GET, POST etc. More...
 
 setHttpVer ($http)
 Sets the HTTP version to use, 1.0 or 1.1. More...
 
 addHeader ($name, $value)
 Adds a request header. More...
 
 removeHeader ($name)
 Removes a request header. More...
 
 addQueryString ($name, $value, $preencoded=false)
 Adds a querystring parameter. More...
 
 addRawQueryString ($querystring, $preencoded=true)
 Sets the querystring to literally what you supply. More...
 
 addPostData ($name, $value, $preencoded=false)
 Adds postdata items. More...
 
 _arrayMapRecursive ($callback, $value)
 Recursively applies the callback function to the value. More...
 
 addFile ($inputName, $fileName, $contentType='application/octet-stream')
 Adds a file to form-based file upload. More...
 
 addRawPostData ($postdata, $preencoded=true)
 Adds raw postdata (DEPRECATED) More...
 
 setBody ($body)
 Sets the request body (for POST, PUT and similar requests) More...
 
 clearPostData ()
 Clears any postdata that has been added (DEPRECATED). More...
 
 addCookie ($name, $value)
 Appends a cookie to "Cookie:" header. More...
 
 clearCookies ()
 Clears any cookies that have been added (DEPRECATED). More...
 
 sendRequest ($saveBody=true)
 Sends the request. More...
 
 disconnect ()
 Disconnect the socket, if connected. More...
 
 getResponseCode ()
 Returns the response code. More...
 
 getResponseReason ()
 Returns the response reason phrase. More...
 
 getResponseHeader ($headername=null)
 Returns either the named header or all if no name given. More...
 
 getResponseBody ()
 Returns the body of the response. More...
 
 getResponseCookies ()
 Returns cookies set in response. More...
 
 _buildRequest ()
 Builds the request string. More...
 
 _flattenArray ($name, $values)
 Helper function to change the (probably multidimensional) associative array into the simple one. More...
 
 attach (&$listener)
 Adds a Listener to the list of listeners that are notified of the object's events. More...
 
 detach (&$listener)
 Removes a Listener from the list of listeners. More...
 
 _notify ($event, $data=null)
 Notifies all registered listeners of an event. More...
 

Data Fields

 $_url
 
 $_method
 
 $_http
 
 $_requestHeaders
 
 $_user
 
 $_pass
 
 $_sock
 
 $_proxy_host
 
 $_proxy_port
 
 $_proxy_user
 
 $_proxy_pass
 
 $_postData
 
 $_body
 
 $_bodyDisallowed = array('TRACE')
 
 $_bodyRequired = array('POST', 'PUT')
 
 $_postFiles = array()
 
 $_timeout
 
 $_response
 
 $_allowRedirects
 
 $_maxRedirects
 
 $_redirects
 
 $_useBrackets = true
 
 $_listeners = array()
 
 $_saveBody = true
 
 $_readTimeout = null
 
 $_socketOptions = null
 

Detailed Description

Definition at line 121 of file Request.php.

Member Function Documentation

◆ _arrayMapRecursive()

HTTP_Request::_arrayMapRecursive (   $callback,
  $value 
)

Recursively applies the callback function to the value.

Parameters
mixedCallback function
mixedValue to process @access private
Returns
mixed Processed value

Definition at line 571 of file Request.php.

572 {
573 if (!is_array($value)) {
574 return call_user_func($callback, $value);
575 } else {
576 $map = array();
577 foreach ($value as $k => $v) {
578 $map[$k] = $this->_arrayMapRecursive($callback, $v);
579 }
580 return $map;
581 }
582 }
_arrayMapRecursive($callback, $value)
Recursively applies the callback function to the value.
Definition: Request.php:571

References _arrayMapRecursive().

Referenced by _arrayMapRecursive(), and addPostData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _buildRequest()

HTTP_Request::_buildRequest ( )

Builds the request string.

@access private

Returns
string The request string

Definition at line 912 of file Request.php.

913 {
914 $separator = ini_get('arg_separator.output');
915 ini_set('arg_separator.output', '&');
916 $querystring = ($querystring = $this->_url->getQueryString()) ? '?' . $querystring : '';
917 ini_set('arg_separator.output', $separator);
918
919 $host = isset($this->_proxy_host) ? $this->_url->protocol . '://' . $this->_url->host : '';
920 $port = (isset($this->_proxy_host) AND $this->_url->port != 80) ? ':' . $this->_url->port : '';
921 $path = $this->_url->path . $querystring;
922 $url = $host . $port . $path;
923
924 if (!strlen($url)) {
925 $url = '/';
926 }
927
928 $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n";
929
930 if (in_array($this->_method, $this->_bodyDisallowed) ||
931 (0 == strlen($this->_body) && (HTTP_REQUEST_METHOD_POST != $this->_method ||
932 (empty($this->_postData) && empty($this->_postFiles)))))
933 {
934 $this->removeHeader('Content-Type');
935 } else {
936 if (empty($this->_requestHeaders['content-type'])) {
937 // Add default content-type
938 $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');
939 } elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) {
940 $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime());
941 $this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary);
942 }
943 }
944
945 // Request Headers
946 if (!empty($this->_requestHeaders)) {
947 foreach ($this->_requestHeaders as $name => $value) {
948 $canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));
949 $request .= $canonicalName . ': ' . $value . "\r\n";
950 }
951 }
952
953 // Method does not allow a body, simply add a final CRLF
954 if (in_array($this->_method, $this->_bodyDisallowed)) {
955
956 $request .= "\r\n";
957
958 // Post data if it's an array
959 } elseif (HTTP_REQUEST_METHOD_POST == $this->_method &&
960 (!empty($this->_postData) || !empty($this->_postFiles))) {
961
962 // "normal" POST request
963 if (!isset($boundary)) {
964 $postdata = implode('&', array_map(
965 create_function('$a', 'return $a[0] . \'=\' . $a[1];'),
966 $this->_flattenArray('', $this->_postData)
967 ));
968
969 // multipart request, probably with file uploads
970 } else {
971 $postdata = '';
972 if (!empty($this->_postData)) {
973 $flatData = $this->_flattenArray('', $this->_postData);
974 foreach ($flatData as $item) {
975 $postdata .= '--' . $boundary . "\r\n";
976 $postdata .= 'Content-Disposition: form-data; name="' . $item[0] . '"';
977 $postdata .= "\r\n\r\n" . urldecode($item[1]) . "\r\n";
978 }
979 }
980 foreach ($this->_postFiles as $name => $value) {
981 if (is_array($value['name'])) {
982 $varname = $name . ($this->_useBrackets? '[]': '');
983 } else {
984 $varname = $name;
985 $value['name'] = array($value['name']);
986 }
987 foreach ($value['name'] as $key => $filename) {
988 $fp = fopen($filename, 'r');
989 $basename = basename($filename);
990 $type = is_array($value['type'])? @$value['type'][$key]: $value['type'];
991
992 $postdata .= '--' . $boundary . "\r\n";
993 $postdata .= 'Content-Disposition: form-data; name="' . $varname . '"; filename="' . $basename . '"';
994 $postdata .= "\r\nContent-Type: " . $type;
995 $postdata .= "\r\n\r\n" . fread($fp, filesize($filename)) . "\r\n";
996 fclose($fp);
997 }
998 }
999 $postdata .= '--' . $boundary . "--\r\n";
1000 }
1001 $request .= 'Content-Length: ' .
1002 (HTTP_REQUEST_MBSTRING? mb_strlen($postdata, 'iso-8859-1'): strlen($postdata)) .
1003 "\r\n\r\n";
1004 $request .= $postdata;
1005
1006 // Explicitly set request body
1007 } elseif (0 < strlen($this->_body)) {
1008
1009 $request .= 'Content-Length: ' .
1010 (HTTP_REQUEST_MBSTRING? mb_strlen($this->_body, 'iso-8859-1'): strlen($this->_body)) .
1011 "\r\n\r\n";
1012 $request .= $this->_body;
1013
1014 // No body: send a Content-Length header nonetheless (request #12900),
1015 // but do that only for methods that require a body (bug #14740)
1016 } else {
1017
1018 if (in_array($this->_method, $this->_bodyRequired)) {
1019 $request .= "Content-Length: 0\r\n";
1020 }
1021 $request .= "\r\n";
1022 }
1023
1024 return $request;
1025 }
const HTTP_REQUEST_METHOD_POST
Definition: Request.php:65
$filename
Definition: buildRTE.php:89
removeHeader($name)
Removes a request header.
Definition: Request.php:514
_flattenArray($name, $values)
Helper function to change the (probably multidimensional) associative array into the simple one.
Definition: Request.php:1036
addHeader($name, $value)
Adds a request header.
Definition: Request.php:503
if(!file_exists(getcwd().'/ilias.ini.php')) if(isset( $_GET["client_id"]))
registration confirmation script for ilias
Definition: confirmReg.php:20
$separator
$url
Definition: shib_logout.php:72
$path
Definition: index.php:22

References $_body, $filename, $path, $separator, $url, _flattenArray(), addHeader(), HTTP_REQUEST_METHOD_POST, if, and removeHeader().

Referenced by sendRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _flattenArray()

HTTP_Request::_flattenArray (   $name,
  $values 
)

Helper function to change the (probably multidimensional) associative array into the simple one.

Parameters
stringname for item
mixeditem's values
Returns
array array with the following items: array('item name', 'item value'); @access private

Definition at line 1036 of file Request.php.

1037 {
1038 if (!is_array($values)) {
1039 return array(array($name, $values));
1040 } else {
1041 $ret = array();
1042 foreach ($values as $k => $v) {
1043 if (empty($name)) {
1044 $newName = $k;
1045 } elseif ($this->_useBrackets) {
1046 $newName = $name . '[' . $k . ']';
1047 } else {
1048 $newName = $name;
1049 }
1050 $ret = array_merge($ret, $this->_flattenArray($newName, $v));
1051 }
1052 return $ret;
1053 }
1054 }

References $ret, and _flattenArray().

Referenced by _buildRequest(), and _flattenArray().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _generateHostHeader()

HTTP_Request::_generateHostHeader ( )

Generates a Host header for HTTP/1.1 requests.

@access private

Returns
string

Definition at line 371 of file Request.php.

372 {
373 if ($this->_url->port != 80 AND strcasecmp($this->_url->protocol, 'http') == 0) {
374 $host = $this->_url->host . ':' . $this->_url->port;
375
376 } elseif ($this->_url->port != 443 AND strcasecmp($this->_url->protocol, 'https') == 0) {
377 $host = $this->_url->host . ':' . $this->_url->port;
378
379 } elseif ($this->_url->port == 443 AND strcasecmp($this->_url->protocol, 'https') == 0 AND strpos($this->_url->url, ':443') !== false) {
380 $host = $this->_url->host . ':' . $this->_url->port;
381
382 } else {
383 $host = $this->_url->host;
384 }
385
386 return $host;
387 }

Referenced by sendRequest(), and setURL().

+ Here is the caller graph for this function:

◆ _notify()

HTTP_Request::_notify (   $event,
  $data = null 
)

Notifies all registered listeners of an event.

Parameters
stringEvent name
mixedAdditional data @access private
See also
HTTP_Request::attach()

Definition at line 1112 of file Request.php.

1113 {
1114 foreach (array_keys($this->_listeners) as $id) {
1115 $this->_listeners[$id]->update($this, $event, $data);
1116 }
1117 }
$data

References $data.

Referenced by disconnect(), and sendRequest().

+ Here is the caller graph for this function:

◆ addCookie()

HTTP_Request::addCookie (   $name,
  $value 
)

Appends a cookie to "Cookie:" header.

Parameters
string$namecookie name
string$valuecookie value @access public

Definition at line 663 of file Request.php.

664 {
665 $cookies = isset($this->_requestHeaders['cookie']) ? $this->_requestHeaders['cookie']. '; ' : '';
666 $this->addHeader('Cookie', $cookies . $name . '=' . $value);
667 }

References addHeader().

+ Here is the call graph for this function:

◆ addFile()

HTTP_Request::addFile (   $inputName,
  $fileName,
  $contentType = 'application/octet-stream' 
)

Adds a file to form-based file upload.

Used to emulate file upload via a HTML form. The method also sets Content-Type of HTTP request to 'multipart/form-data'.

If you just want to send the contents of a file as the body of HTTP request you should use setBody() method.

@access public

Parameters
stringname of file-upload field
mixedfile name(s)
mixedcontent-type(s) of file(s) being uploaded
Returns
bool true on success
Exceptions
PEAR_Error

Definition at line 600 of file Request.php.

601 {
602 if (!is_array($fileName) && !is_readable($fileName)) {
603 return PEAR::raiseError("File '{$fileName}' is not readable", HTTP_REQUEST_ERROR_FILE);
604 } elseif (is_array($fileName)) {
605 foreach ($fileName as $name) {
606 if (!is_readable($name)) {
607 return PEAR::raiseError("File '{$name}' is not readable", HTTP_REQUEST_ERROR_FILE);
608 }
609 }
610 }
611 $this->addHeader('Content-Type', 'multipart/form-data');
612 $this->_postFiles[$inputName] = array(
613 'name' => $fileName,
614 'type' => $contentType
615 );
616 return true;
617 }
const HTTP_REQUEST_ERROR_FILE
#-
Definition: Request.php:75
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
Definition: PEAR.php:524

References addHeader(), HTTP_REQUEST_ERROR_FILE, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ addHeader()

HTTP_Request::addHeader (   $name,
  $value 
)

Adds a request header.

Parameters
stringHeader name
stringHeader value @access public

Definition at line 503 of file Request.php.

504 {
505 $this->_requestHeaders[strtolower($name)] = $value;
506 }

Referenced by _buildRequest(), addCookie(), addFile(), HTTP_Request(), sendRequest(), setBasicAuth(), setProxy(), and setURL().

+ Here is the caller graph for this function:

◆ addPostData()

HTTP_Request::addPostData (   $name,
  $value,
  $preencoded = false 
)

Adds postdata items.

Parameters
stringPost data name
stringPost data value
boolWhether data is already urlencoded or not, default = not @access public

Definition at line 554 of file Request.php.

555 {
556 if ($preencoded) {
557 $this->_postData[$name] = $value;
558 } else {
559 $this->_postData[$name] = $this->_arrayMapRecursive('urlencode', $value);
560 }
561 }

References _arrayMapRecursive().

+ Here is the call graph for this function:

◆ addQueryString()

HTTP_Request::addQueryString (   $name,
  $value,
  $preencoded = false 
)

Adds a querystring parameter.

Parameters
stringQuerystring parameter name
stringQuerystring parameter value
boolWhether the value is already urlencoded or not, default = not @access public

Definition at line 529 of file Request.php.

530 {
531 $this->_url->addQueryString($name, $value, $preencoded);
532 }

◆ addRawPostData()

HTTP_Request::addRawPostData (   $postdata,
  $preencoded = true 
)

Adds raw postdata (DEPRECATED)

Parameters
stringThe data
boolWhether data is preencoded or not, default = already encoded @access public
Deprecated:
deprecated since 1.3.0, method setBody() should be used instead

Definition at line 627 of file Request.php.

628 {
629 $this->_body = $preencoded ? $postdata : urlencode($postdata);
630 }

◆ addRawQueryString()

HTTP_Request::addRawQueryString (   $querystring,
  $preencoded = true 
)

Sets the querystring to literally what you supply.

Parameters
stringThe querystring data. Should be of the format foo=bar&x=y etc
boolWhether data is already urlencoded or not, default = already encoded @access public

Definition at line 541 of file Request.php.

542 {
543 $this->_url->addRawQueryString($querystring, $preencoded);
544 }

◆ attach()

HTTP_Request::attach ( $listener)

Adds a Listener to the list of listeners that are notified of the object's events.

Events sent by HTTP_Request object

  • 'connect': on connection to server
  • 'sentRequest': after the request was sent
  • 'disconnect': on disconnection from server

Events sent by HTTP_Response object

  • 'gotHeaders': after receiving response headers (headers are passed in $data)
  • 'tick': on receiving a part of response body (the part is passed in $data)
  • 'gzTick': on receiving a gzip-encoded part of response body (ditto)
  • 'gotBody': after receiving the response body (passes the decoded body in $data if it was gzipped)
Parameters
HTTP_Request_Listenerlistener to attach
Returns
boolean whether the listener was successfully attached @access public

Definition at line 1076 of file Request.php.

1077 {
1078 if (!is_a($listener, 'HTTP_Request_Listener')) {
1079 return false;
1080 }
1081 $this->_listeners[$listener->getId()] =& $listener;
1082 return true;
1083 }

◆ clearCookies()

HTTP_Request::clearCookies ( )

Clears any cookies that have been added (DEPRECATED).

Useful for multiple request scenarios

@access public

Deprecated:
deprecated since 1.2

Definition at line 677 of file Request.php.

678 {
679 $this->removeHeader('Cookie');
680 }

References removeHeader().

+ Here is the call graph for this function:

◆ clearPostData()

HTTP_Request::clearPostData ( )

Clears any postdata that has been added (DEPRECATED).

Useful for multiple request scenarios.

@access public

Deprecated:
deprecated since 1.2

Definition at line 651 of file Request.php.

652 {
653 $this->_postData = null;
654 }

◆ detach()

HTTP_Request::detach ( $listener)

Removes a Listener from the list of listeners.

Parameters
HTTP_Request_Listenerlistener to detach
Returns
boolean whether the listener was successfully detached @access public

Definition at line 1093 of file Request.php.

1094 {
1095 if (!is_a($listener, 'HTTP_Request_Listener') ||
1096 !isset($this->_listeners[$listener->getId()])) {
1097 return false;
1098 }
1099 unset($this->_listeners[$listener->getId()]);
1100 return true;
1101 }

◆ disconnect()

HTTP_Request::disconnect ( )

Disconnect the socket, if connected.

Only useful if using Keep-Alive.

@access public

Definition at line 836 of file Request.php.

837 {
838 if (!empty($this->_sock) && !empty($this->_sock->fp)) {
839 $this->_notify('disconnect');
840 $this->_sock->disconnect();
841 }
842 }
_notify($event, $data=null)
Notifies all registered listeners of an event.
Definition: Request.php:1112

References _notify().

Referenced by sendRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getResponseBody()

HTTP_Request::getResponseBody ( )

Returns the body of the response.

@access public

Returns
mixed response body, false if not set

Definition at line 890 of file Request.php.

891 {
892 return isset($this->_response->_body) ? $this->_response->_body : false;
893 }

◆ getResponseCode()

HTTP_Request::getResponseCode ( )

Returns the response code.

@access public

Returns
mixed Response code, false if not set

Definition at line 850 of file Request.php.

851 {
852 return isset($this->_response->_code) ? $this->_response->_code : false;
853 }

Referenced by sendRequest().

+ Here is the caller graph for this function:

◆ getResponseCookies()

HTTP_Request::getResponseCookies ( )

Returns cookies set in response.

@access public

Returns
mixed array of response cookies, false if none are present

Definition at line 901 of file Request.php.

902 {
903 return isset($this->_response->_cookies) ? $this->_response->_cookies : false;
904 }

◆ getResponseHeader()

HTTP_Request::getResponseHeader (   $headername = null)

Returns either the named header or all if no name given.

@access public

Parameters
stringThe header name to return, do not set to get all headers
Returns
mixed either the value of $headername (false if header is not present) or an array of all headers

Definition at line 874 of file Request.php.

875 {
876 if (!isset($headername)) {
877 return isset($this->_response->_headers)? $this->_response->_headers: array();
878 } else {
879 $headername = strtolower($headername);
880 return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false;
881 }
882 }

◆ getResponseReason()

HTTP_Request::getResponseReason ( )

Returns the response reason phrase.

@access public

Returns
mixed Response reason phrase, false if not set

Definition at line 861 of file Request.php.

862 {
863 return isset($this->_response->_reason) ? $this->_response->_reason : false;
864 }

◆ getUrl()

HTTP_Request::getUrl ( )

Returns the current request URL.

Returns
string Current request URL @access public

Definition at line 434 of file Request.php.

435 {
436 return empty($this->_url)? '': $this->_url->getUrl();
437 }

◆ HTTP_Request()

HTTP_Request::HTTP_Request (   $url = '',
  $params = array() 
)

#-

Constructor

Sets up the object

Parameters
stringThe url to fetch/access
arrayAssociative array of parameters which can have the following keys:
  • method - Method to use, GET, POST etc (string)
  • http - HTTP Version to use, 1.0 or 1.1 (string)
  • user - Basic Auth username (string)
  • pass - Basic Auth password (string)
  • proxy_host - Proxy server host (string)
  • proxy_port - Proxy server port (integer)
  • proxy_user - Proxy auth username (string)
  • proxy_pass - Proxy auth password (string)
  • timeout - Connection timeout in seconds (float)
  • allowRedirects - Whether to follow redirects or not (bool)
  • maxRedirects - Max number of redirects to follow (integer)
  • useBrackets - Whether to append [] to array variable names (bool)
  • saveBody - Whether to save response body in response object property (bool)
  • readTimeout - Timeout for reading / writing data over the socket (array (seconds, microseconds))
  • socketOptions - Options to pass to Net_Socket object (array)
@access public

Definition at line 312 of file Request.php.

313 {
314 $this->_method = HTTP_REQUEST_METHOD_GET;
315 $this->_http = HTTP_REQUEST_HTTP_VER_1_1;
316 $this->_requestHeaders = array();
317 $this->_postData = array();
318 $this->_body = null;
319
320 $this->_user = null;
321 $this->_pass = null;
322
323 $this->_proxy_host = null;
324 $this->_proxy_port = null;
325 $this->_proxy_user = null;
326 $this->_proxy_pass = null;
327
328 $this->_allowRedirects = false;
329 $this->_maxRedirects = 3;
330 $this->_redirects = 0;
331
332 $this->_timeout = null;
333 $this->_response = null;
334
335 foreach ($params as $key => $value) {
336 $this->{'_' . $key} = $value;
337 }
338
339 if (!empty($url)) {
340 $this->setURL($url);
341 }
342
343 // Default useragent
344 $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )');
345
346 // We don't do keep-alives by default
347 $this->addHeader('Connection', 'close');
348
349 // Basic authentication
350 if (!empty($this->_user)) {
351 $this->addHeader('Authorization', 'Basic ' . base64_encode($this->_user . ':' . $this->_pass));
352 }
353
354 // Proxy authentication (see bug #5913)
355 if (!empty($this->_proxy_user)) {
356 $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($this->_proxy_user . ':' . $this->_proxy_pass));
357 }
358
359 // Use gzip encoding if possible
360 if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib')) {
361 $this->addHeader('Accept-Encoding', 'gzip');
362 }
363 }
const HTTP_REQUEST_HTTP_VER_1_1
Definition: Request.php:90
const HTTP_REQUEST_METHOD_GET
PEAR and PEAR_Error classes (for error handling)
Definition: Request.php:63
setURL($url)
Sets the URL to be requested.
Definition: Request.php:410
$params
Definition: example_049.php:96

References $params, $url, addHeader(), HTTP_REQUEST_HTTP_VER_1_1, HTTP_REQUEST_METHOD_GET, and setURL().

Referenced by reset().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeHeader()

HTTP_Request::removeHeader (   $name)

Removes a request header.

Parameters
stringHeader name to remove @access public

Definition at line 514 of file Request.php.

515 {
516 if (isset($this->_requestHeaders[strtolower($name)])) {
517 unset($this->_requestHeaders[strtolower($name)]);
518 }
519 }

Referenced by _buildRequest(), clearCookies(), and sendRequest().

+ Here is the caller graph for this function:

◆ reset()

HTTP_Request::reset (   $url,
  $params = array() 
)

Resets the object to its initial state (DEPRECATED).

Takes the same parameters as the constructor.

Parameters
string$urlThe url to be requested
array$paramsAssociative array of parameters (see constructor for details) @access public
Deprecated:
deprecated since 1.2, call the constructor if this is necessary

Definition at line 399 of file Request.php.

400 {
401 $this->HTTP_Request($url, $params);
402 }
HTTP_Request($url='', $params=array())
#-
Definition: Request.php:312

References $params, $url, and HTTP_Request().

+ Here is the call graph for this function:

◆ sendRequest()

HTTP_Request::sendRequest (   $saveBody = true)

Sends the request.

@access public

Parameters
boolWhether to store response body in Response object property, set this to false if downloading a LARGE file and using a Listener
Returns
mixed PEAR error on error, true otherwise

Definition at line 690 of file Request.php.

691 {
692 if (!is_a($this->_url, 'Net_URL')) {
693 return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL);
694 }
695
696 $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host;
697 $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port;
698
699 if (strcasecmp($this->_url->protocol, 'https') == 0) {
700 // Bug #14127, don't try connecting to HTTPS sites without OpenSSL
701 if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) {
702 return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests',
704 } elseif (isset($this->_proxy_host)) {
705 return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY);
706 }
707 $host = 'ssl://' . $host;
708 }
709
710 // magic quotes may fuck up file uploads and chunked response processing
711 $magicQuotes = ini_get('magic_quotes_runtime');
712 ini_set('magic_quotes_runtime', false);
713
714 // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive
715 // connection token to a proxy server...
716 if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) &&
717 'Keep-Alive' == $this->_requestHeaders['connection'])
718 {
719 $this->removeHeader('connection');
720 }
721
722 $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) ||
723 (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']);
724 $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets');
725 $sockKey = $host . ':' . $port;
726 unset($this->_sock);
727
728 // There is a connected socket in the "static" property?
729 if ($keepAlive && !empty($sockets[$sockKey]) &&
730 !empty($sockets[$sockKey]->fp))
731 {
732 $this->_sock =& $sockets[$sockKey];
733 $err = null;
734 } else {
735 $this->_notify('connect');
736 $this->_sock =& new Net_Socket();
737 $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions);
738 }
739 PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest());
740
741 if (!PEAR::isError($err)) {
742 if (!empty($this->_readTimeout)) {
743 $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]);
744 }
745
746 $this->_notify('sentRequest');
747
748 // Read the response
749 $this->_response = &new HTTP_Response($this->_sock, $this->_listeners);
750 $err = $this->_response->process(
751 $this->_saveBody && $saveBody,
752 HTTP_REQUEST_METHOD_HEAD != $this->_method
753 );
754
755 if ($keepAlive) {
756 $keepAlive = (isset($this->_response->_headers['content-length'])
757 || (isset($this->_response->_headers['transfer-encoding'])
758 && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked'));
759 if ($keepAlive) {
760 if (isset($this->_response->_headers['connection'])) {
761 $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive';
762 } else {
763 $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol;
764 }
765 }
766 }
767 }
768
769 ini_set('magic_quotes_runtime', $magicQuotes);
770
771 if (PEAR::isError($err)) {
772 return $err;
773 }
774
775 if (!$keepAlive) {
776 $this->disconnect();
777 // Store the connected socket in "static" property
778 } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) {
779 $sockets[$sockKey] =& $this->_sock;
780 }
781
782 // Check for redirection
783 if ( $this->_allowRedirects
784 AND $this->_redirects <= $this->_maxRedirects
785 AND $this->getResponseCode() > 300
786 AND $this->getResponseCode() < 399
787 AND !empty($this->_response->_headers['location'])) {
788
789
790 $redirect = $this->_response->_headers['location'];
791
792 // Absolute URL
793 if (preg_match('/^https?:\/\//i', $redirect)) {
794 $this->_url = &new Net_URL($redirect);
795 $this->addHeader('Host', $this->_generateHostHeader());
796 // Absolute path
797 } elseif ($redirect{0} == '/') {
798 $this->_url->path = $redirect;
799
800 // Relative path
801 } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') {
802 if (substr($this->_url->path, -1) == '/') {
803 $redirect = $this->_url->path . $redirect;
804 } else {
805 $redirect = dirname($this->_url->path) . '/' . $redirect;
806 }
808 $this->_url->path = $redirect;
809
810 // Filename, no path
811 } else {
812 if (substr($this->_url->path, -1) == '/') {
813 $redirect = $this->_url->path . $redirect;
814 } else {
815 $redirect = dirname($this->_url->path) . '/' . $redirect;
816 }
817 $this->_url->path = $redirect;
818 }
819
820 $this->_redirects++;
821 return $this->sendRequest($saveBody);
822
823 // Too many redirects
824 } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) {
825 return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS);
826 }
827
828 return true;
829 }
const HTTP_REQUEST_ERROR_PROXY
Definition: Request.php:77
const HTTP_REQUEST_ERROR_REDIRECTS
Definition: Request.php:78
const HTTP_REQUEST_ERROR_URL
Definition: Request.php:76
const HTTP_REQUEST_METHOD_HEAD
Definition: Request.php:64
_buildRequest()
Builds the request string.
Definition: Request.php:912
_generateHostHeader()
Generates a Host header for HTTP/1.1 requests.
Definition: Request.php:371
disconnect()
Disconnect the socket, if connected.
Definition: Request.php:836
getResponseCode()
Returns the response code.
Definition: Request.php:850
sendRequest($saveBody=true)
Sends the request.
Definition: Request.php:690
Generalized Socket class.
Definition: Socket.php:35
Definition: URL.php:41
resolvePath($path)
Resolves //, ../ and .
Definition: URL.php:381
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
& getStaticProperty($class, $var)
If you have a class that's mostly/entirely static, and you need static properties,...
Definition: PEAR.php:230
$redirect
Definition: index.php:18

References $_sock, $redirect, _buildRequest(), _generateHostHeader(), _notify(), addHeader(), disconnect(), getResponseCode(), PEAR\getStaticProperty(), HTTP_REQUEST_ERROR_PROXY, HTTP_REQUEST_ERROR_REDIRECTS, HTTP_REQUEST_ERROR_URL, HTTP_REQUEST_HTTP_VER_1_1, HTTP_REQUEST_METHOD_HEAD, PEAR\isError(), PEAR\raiseError(), removeHeader(), Net_URL\resolvePath(), and sendRequest().

Referenced by sendRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBasicAuth()

HTTP_Request::setBasicAuth (   $user,
  $pass 
)

Sets basic authentication parameters.

Parameters
stringUsername
stringPassword

Definition at line 466 of file Request.php.

467 {
468 $this->_user = $user;
469 $this->_pass = $pass;
470
471 $this->addHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass));
472 }

References $pass, and addHeader().

Referenced by setURL().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBody()

HTTP_Request::setBody (   $body)

Sets the request body (for POST, PUT and similar requests)

Parameters
stringRequest body @access public

Definition at line 638 of file Request.php.

639 {
640 $this->_body = $body;
641 }

◆ setHttpVer()

HTTP_Request::setHttpVer (   $http)

Sets the HTTP version to use, 1.0 or 1.1.

Parameters
stringVersion to use. Use the defined constants for this @access public

Definition at line 491 of file Request.php.

492 {
493 $this->_http = $http;
494 }

◆ setMethod()

HTTP_Request::setMethod (   $method)

Sets the method to be used, GET, POST etc.

Parameters
stringMethod to use. Use the defined constants for this @access public

Definition at line 480 of file Request.php.

481 {
482 $this->_method = $method;
483 }

◆ setProxy()

HTTP_Request::setProxy (   $host,
  $port = 8080,
  $user = null,
  $pass = null 
)

Sets a proxy to be used.

Parameters
stringProxy host
intProxy port
stringProxy username
stringProxy password @access public

Definition at line 448 of file Request.php.

449 {
450 $this->_proxy_host = $host;
451 $this->_proxy_port = $port;
452 $this->_proxy_user = $user;
453 $this->_proxy_pass = $pass;
454
455 if (!empty($user)) {
456 $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $pass));
457 }
458 }

References $pass, and addHeader().

+ Here is the call graph for this function:

◆ setURL()

HTTP_Request::setURL (   $url)

Sets the URL to be requested.

Parameters
stringThe url to be requested @access public

Definition at line 410 of file Request.php.

411 {
412 $this->_url = &new Net_URL($url, $this->_useBrackets);
413
414 if (!empty($this->_url->user) || !empty($this->_url->pass)) {
415 $this->setBasicAuth($this->_url->user, $this->_url->pass);
416 }
417
418 if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http) {
419 $this->addHeader('Host', $this->_generateHostHeader());
420 }
421
422 // set '/' instead of empty path rather than check later (see bug #8662)
423 if (empty($this->_url->path)) {
424 $this->_url->path = '/';
425 }
426 }
setBasicAuth($user, $pass)
Sets basic authentication parameters.
Definition: Request.php:466

References $url, _generateHostHeader(), addHeader(), HTTP_REQUEST_HTTP_VER_1_1, and setBasicAuth().

Referenced by HTTP_Request().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_allowRedirects

HTTP_Request::$_allowRedirects

Definition at line 242 of file Request.php.

◆ $_body

HTTP_Request::$_body

Definition at line 202 of file Request.php.

Referenced by _buildRequest().

◆ $_bodyDisallowed

HTTP_Request::$_bodyDisallowed = array('TRACE')

Definition at line 208 of file Request.php.

◆ $_bodyRequired

HTTP_Request::$_bodyRequired = array('POST', 'PUT')

Definition at line 218 of file Request.php.

◆ $_http

HTTP_Request::$_http

Definition at line 142 of file Request.php.

◆ $_listeners

HTTP_Request::$_listeners = array()

Definition at line 266 of file Request.php.

◆ $_maxRedirects

HTTP_Request::$_maxRedirects

Definition at line 248 of file Request.php.

◆ $_method

HTTP_Request::$_method

Definition at line 136 of file Request.php.

◆ $_pass

HTTP_Request::$_pass

Definition at line 160 of file Request.php.

◆ $_postData

HTTP_Request::$_postData

Definition at line 196 of file Request.php.

◆ $_postFiles

HTTP_Request::$_postFiles = array()

Definition at line 224 of file Request.php.

◆ $_proxy_host

HTTP_Request::$_proxy_host

Definition at line 172 of file Request.php.

◆ $_proxy_pass

HTTP_Request::$_proxy_pass

Definition at line 190 of file Request.php.

◆ $_proxy_port

HTTP_Request::$_proxy_port

Definition at line 178 of file Request.php.

◆ $_proxy_user

HTTP_Request::$_proxy_user

Definition at line 184 of file Request.php.

◆ $_readTimeout

HTTP_Request::$_readTimeout = null

Definition at line 278 of file Request.php.

◆ $_redirects

HTTP_Request::$_redirects

Definition at line 254 of file Request.php.

◆ $_requestHeaders

HTTP_Request::$_requestHeaders

Definition at line 148 of file Request.php.

◆ $_response

HTTP_Request::$_response

Definition at line 236 of file Request.php.

◆ $_saveBody

HTTP_Request::$_saveBody = true

Definition at line 272 of file Request.php.

◆ $_sock

HTTP_Request::$_sock

Definition at line 166 of file Request.php.

Referenced by sendRequest().

◆ $_socketOptions

HTTP_Request::$_socketOptions = null

Definition at line 284 of file Request.php.

◆ $_timeout

HTTP_Request::$_timeout

Definition at line 230 of file Request.php.

◆ $_url

HTTP_Request::$_url

Definition at line 130 of file Request.php.

◆ $_useBrackets

HTTP_Request::$_useBrackets = true

Definition at line 260 of file Request.php.

◆ $_user

HTTP_Request::$_user

Definition at line 154 of file Request.php.


The documentation for this class was generated from the following file: