Source for file http.php

Documentation is available at http.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  HTTP
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * HTTP client class.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  HTTP
  17.  * @since       11.3
  18.  */
  19. class JHttp
  20. {
  21.     /**
  22.      * @var    JRegistry  Options for the HTTP client.
  23.      * @since  11.3
  24.      */
  25.     protected $options;
  26.  
  27.     /**
  28.      * @var    JHttpTransport  The HTTP transport object to use in sending HTTP requests.
  29.      * @since  11.3
  30.      */
  31.     protected $transport;
  32.  
  33.     /**
  34.      * Constructor.
  35.      *
  36.      * @param   JRegistry       $options    Client options object. If the registry contains any headers.* elements,
  37.      *                                       these will be added to the request headers.
  38.      * @param   JHttpTransport  $transport  The HTTP transport object.
  39.      *
  40.      * @since   11.3
  41.      */
  42.     public function __construct(JRegistry $options nullJHttpTransport $transport null)
  43.     {
  44.         $this->options   = isset($options$options new JRegistry;
  45.         $this->transport = isset($transport$transport JHttpFactory::getAvailableDriver($this->options);
  46.     }
  47.  
  48.     /**
  49.      * Get an option from the HTTP client.
  50.      *
  51.      * @param   string  $key  The name of the option to get.
  52.      *
  53.      * @return  mixed  The option value.
  54.      *
  55.      * @since   11.3
  56.      */
  57.     public function getOption($key)
  58.     {
  59.         return $this->options->get($key);
  60.     }
  61.  
  62.     /**
  63.      * Set an option for the HTTP client.
  64.      *
  65.      * @param   string  $key    The name of the option to set.
  66.      * @param   mixed   $value  The option value to set.
  67.      *
  68.      * @return  JHttp  This object for method chaining.
  69.      *
  70.      * @since   11.3
  71.      */
  72.     public function setOption($key$value)
  73.     {
  74.         $this->options->set($key$value);
  75.  
  76.         return $this;
  77.     }
  78.  
  79.     /**
  80.      * Method to send the OPTIONS command to the server.
  81.      *
  82.      * @param   string   $url      Path to the resource.
  83.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  84.      * @param   integer  $timeout  Read timeout in seconds.
  85.      *
  86.      * @return  JHttpResponse 
  87.      *
  88.      * @since   11.3
  89.      */
  90.     public function options($urlarray $headers null$timeout null)
  91.     {
  92.         // Look for headers set in the options.
  93.         $temp = (array) $this->options->get('headers');
  94.  
  95.         foreach ($temp as $key => $val)
  96.         {
  97.             if (!isset($headers[$key]))
  98.             {
  99.                 $headers[$key$val;
  100.             }
  101.         }
  102.  
  103.         // Look for timeout set in the options.
  104.         if ($timeout === null && $this->options->exists('timeout'))
  105.         {
  106.             $timeout $this->options->get('timeout');
  107.         }
  108.  
  109.         return $this->transport->request('OPTIONS'new JUri($url)null$headers$timeout$this->options->get('userAgent'null));
  110.     }
  111.  
  112.     /**
  113.      * Method to send the HEAD command to the server.
  114.      *
  115.      * @param   string   $url      Path to the resource.
  116.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  117.      * @param   integer  $timeout  Read timeout in seconds.
  118.      *
  119.      * @return  JHttpResponse 
  120.      *
  121.      * @since   11.3
  122.      */
  123.     public function head($urlarray $headers null$timeout null)
  124.     {
  125.         // Look for headers set in the options.
  126.         $temp = (array) $this->options->get('headers');
  127.  
  128.         foreach ($temp as $key => $val)
  129.         {
  130.             if (!isset($headers[$key]))
  131.             {
  132.                 $headers[$key$val;
  133.             }
  134.         }
  135.  
  136.         // Look for timeout set in the options.
  137.         if ($timeout === null && $this->options->exists('timeout'))
  138.         {
  139.             $timeout $this->options->get('timeout');
  140.         }
  141.  
  142.         return $this->transport->request('HEAD'new JUri($url)null$headers$timeout$this->options->get('userAgent'null));
  143.     }
  144.  
  145.     /**
  146.      * Method to send the GET command to the server.
  147.      *
  148.      * @param   string   $url      Path to the resource.
  149.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  150.      * @param   integer  $timeout  Read timeout in seconds.
  151.      *
  152.      * @return  JHttpResponse 
  153.      *
  154.      * @since   11.3
  155.      */
  156.     public function get($urlarray $headers null$timeout null)
  157.     {
  158.         // Look for headers set in the options.
  159.         $temp = (array) $this->options->get('headers');
  160.  
  161.         foreach ($temp as $key => $val)
  162.         {
  163.             if (!isset($headers[$key]))
  164.             {
  165.                 $headers[$key$val;
  166.             }
  167.         }
  168.  
  169.         // Look for timeout set in the options.
  170.         if ($timeout === null && $this->options->exists('timeout'))
  171.         {
  172.             $timeout $this->options->get('timeout');
  173.         }
  174.  
  175.         return $this->transport->request('GET'new JUri($url)null$headers$timeout$this->options->get('userAgent'null));
  176.     }
  177.  
  178.     /**
  179.      * Method to send the POST command to the server.
  180.      *
  181.      * @param   string   $url      Path to the resource.
  182.      * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  183.      * @param   array    $headers  An array of name-value pairs to include in the header of the request
  184.      * @param   integer  $timeout  Read timeout in seconds.
  185.      *
  186.      * @return  JHttpResponse 
  187.      *
  188.      * @since   11.3
  189.      */
  190.     public function post($url$dataarray $headers null$timeout null)
  191.     {
  192.         // Look for headers set in the options.
  193.         $temp = (array) $this->options->get('headers');
  194.  
  195.         foreach ($temp as $key => $val)
  196.         {
  197.             if (!isset($headers[$key]))
  198.             {
  199.                 $headers[$key$val;
  200.             }
  201.         }
  202.  
  203.         // Look for timeout set in the options.
  204.         if ($timeout === null && $this->options->exists('timeout'))
  205.         {
  206.             $timeout $this->options->get('timeout');
  207.         }
  208.  
  209.         return $this->transport->request('POST'new JUri($url)$data$headers$timeout$this->options->get('userAgent'null));
  210.     }
  211.  
  212.     /**
  213.      * Method to send the PUT command to the server.
  214.      *
  215.      * @param   string   $url      Path to the resource.
  216.      * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  217.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  218.      * @param   integer  $timeout  Read timeout in seconds.
  219.      *
  220.      * @return  JHttpResponse 
  221.      *
  222.      * @since   11.3
  223.      */
  224.     public function put($url$dataarray $headers null$timeout null)
  225.     {
  226.         // Look for headers set in the options.
  227.         $temp = (array) $this->options->get('headers');
  228.  
  229.         foreach ($temp as $key => $val)
  230.         {
  231.             if (!isset($headers[$key]))
  232.             {
  233.                 $headers[$key$val;
  234.             }
  235.         }
  236.  
  237.         // Look for timeout set in the options.
  238.         if ($timeout === null && $this->options->exists('timeout'))
  239.         {
  240.             $timeout $this->options->get('timeout');
  241.         }
  242.  
  243.         return $this->transport->request('PUT'new JUri($url)$data$headers$timeout$this->options->get('userAgent'null));
  244.     }
  245.  
  246.     /**
  247.      * Method to send the DELETE command to the server.
  248.      *
  249.      * @param   string   $url      Path to the resource.
  250.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  251.      * @param   integer  $timeout  Read timeout in seconds.
  252.      *
  253.      * @return  JHttpResponse 
  254.      *
  255.      * @since   11.3
  256.      */
  257.     public function delete($urlarray $headers null$timeout null)
  258.     {
  259.         // Look for headers set in the options.
  260.         $temp = (array) $this->options->get('headers');
  261.  
  262.         foreach ($temp as $key => $val)
  263.         {
  264.             if (!isset($headers[$key]))
  265.             {
  266.                 $headers[$key$val;
  267.             }
  268.         }
  269.  
  270.         // Look for timeout set in the options.
  271.         if ($timeout === null && $this->options->exists('timeout'))
  272.         {
  273.             $timeout $this->options->get('timeout');
  274.         }
  275.  
  276.         return $this->transport->request('DELETE'new JUri($url)null$headers$timeout$this->options->get('userAgent'null));
  277.     }
  278.  
  279.     /**
  280.      * Method to send the TRACE command to the server.
  281.      *
  282.      * @param   string   $url      Path to the resource.
  283.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  284.      * @param   integer  $timeout  Read timeout in seconds.
  285.      *
  286.      * @return  JHttpResponse 
  287.      *
  288.      * @since   11.3
  289.      */
  290.     public function trace($urlarray $headers null$timeout null)
  291.     {
  292.         // Look for headers set in the options.
  293.         $temp = (array) $this->options->get('headers');
  294.  
  295.         foreach ($temp as $key => $val)
  296.         {
  297.             if (!isset($headers[$key]))
  298.             {
  299.                 $headers[$key$val;
  300.             }
  301.         }
  302.  
  303.         // Look for timeout set in the options.
  304.         if ($timeout === null && $this->options->exists('timeout'))
  305.         {
  306.             $timeout $this->options->get('timeout');
  307.         }
  308.  
  309.         return $this->transport->request('TRACE'new JUri($url)null$headers$timeout$this->options->get('userAgent'null));
  310.     }
  311.  
  312.     /**
  313.      * Method to send the PATCH command to the server.
  314.      *
  315.      * @param   string   $url      Path to the resource.
  316.      * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  317.      * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  318.      * @param   integer  $timeout  Read timeout in seconds.
  319.      *
  320.      * @return  JHttpResponse 
  321.      *
  322.      * @since   12.2
  323.      */
  324.     public function patch($url$dataarray $headers null$timeout null)
  325.     {
  326.         // Look for headers set in the options.
  327.         $temp = (array) $this->options->get('headers');
  328.  
  329.         foreach ($temp as $key => $val)
  330.         {
  331.             if (!isset($headers[$key]))
  332.             {
  333.                 $headers[$key$val;
  334.             }
  335.         }
  336.  
  337.         // Look for timeout set in the options.
  338.         if ($timeout === null && $this->options->exists('timeout'))
  339.         {
  340.             $timeout $this->options->get('timeout');
  341.         }
  342.  
  343.         return $this->transport->request('PATCH'new JUri($url)$data$headers$timeout$this->options->get('userAgent'null));
  344.     }
  345. }

Documentation generated on Tue, 19 Nov 2013 15:04:56 +0100 by phpDocumentor 1.4.3