Microsoft_Http
[ class tree: Microsoft_Http ] [ index: Microsoft_Http ] [ all elements ]

Source for file Curl.php

Documentation is available at Curl.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_Http
  30.  * @subpackage Transport
  31.  * @version    $Id: Curl.php 22249 2009-06-18 09:49:55Z unknown $
  32.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  33.  * @license    http://phpazure.codeplex.com/license
  34.  */
  35.  
  36. /**
  37.  * @see Microsoft_Http_Transport_Exception
  38.  */
  39. require_once 'Microsoft/Http/Transport/Exception.php';
  40.  
  41. /**
  42.  * @see Microsoft_Http_Transport
  43.  */
  44. require_once 'Microsoft/Http/Transport.php';
  45.  
  46. /**
  47.  * @see Microsoft_Http_Response
  48.  */
  49. require_once 'Microsoft/Http/Response.php';
  50.  
  51. /**
  52.  * @category   Microsoft
  53.  * @package    Microsoft_Http
  54.  * @subpackage Transport
  55.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  56.  * @license    http://phpazure.codeplex.com/license
  57.  */
  58. {
  59.     /**
  60.      * Microsoft_Http_Transport_Curl constructor
  61.      */
  62.     public function __construct(
  63.     {
  64.         if (!extension_loaded('curl')) {
  65.             throw new Microsoft_Http_Transport_Exception('cURL extension has to be loaded to use Microsoft_Http_Transport_Curl.');
  66.         }
  67.     }
  68.     
  69.     /**
  70.      * Perform request
  71.      * 
  72.      * @param $httpVerb         Http verb to use in the request
  73.      * @param $url              Url to request
  74.      * @param $variables        Array of key-value pairs to use in the request
  75.      * @param $headers          Array of key-value pairs to use as additional headers
  76.      * @param $rawBody          Raw body to send to server
  77.      * @return Microsoft_Http_Response 
  78.      */
  79.     public function request($httpVerb$url$variables array()$headers array()$rawBody null)
  80.     {
  81.         // Create a new cURL instance
  82.         $curlHandle curl_init();
  83.         curl_setopt($curlHandleCURLOPT_USERAGENT,       $this->_userAgent);
  84.         curl_setopt($curlHandleCURLOPT_FOLLOWLOCATION,  true);
  85.         curl_setopt($curlHandleCURLOPT_TIMEOUT,         120);
  86.  
  87.         // Set URL
  88.         curl_setopt($curlHandleCURLOPT_URL,             $url);
  89.         
  90.         // Set HTTP parameters (version and request method)
  91.         curl_setopt($curlHandleCURL_HTTP_VERSION_1_1,   true);
  92.         switch ($httpVerb{
  93.             case Microsoft_Http_Transport::VERB_GET:
  94.                 curl_setopt($curlHandleCURLOPT_HTTPGETtrue);
  95.                 break;
  96.             case Microsoft_Http_Transport::VERB_POST:
  97.                 curl_setopt($curlHandleCURLOPT_POST,    true);
  98.                 break;
  99.             /*case Microsoft_Http_Transport::VERB_PUT:
  100.                 curl_setopt($curlHandle, CURLOPT_PUT,     true);
  101.                 break;*/
  102.             case Microsoft_Http_Transport::VERB_HEAD:
  103.                 // http://stackoverflow.com/questions/770179/php-curl-head-request-takes-a-long-time-on-some-sites
  104.                 curl_setopt($curlHandleCURLOPT_CUSTOMREQUEST,  'HEAD');
  105.                 curl_setopt($curlHandleCURLOPT_NOBODYtrue);
  106.                 break;
  107.             default:
  108.                 curl_setopt($curlHandleCURLOPT_CUSTOMREQUEST,  $httpVerb);
  109.                 break;
  110.         }
  111.  
  112.         // Clear Content-Length header
  113.         $headers["Content-Length"0;
  114.  
  115.         // Ensure headers are returned
  116.         curl_setopt($curlHandleCURLOPT_HEADER,          true);
  117.         
  118.         // Set proxy?
  119.         if ($this->_useProxy)
  120.         {
  121.             curl_setopt($curlHandleCURLOPT_PROXY,        $this->_proxyUrl)
  122.             curl_setopt($curlHandleCURLOPT_PROXYPORT,    $this->_proxyPort)
  123.             curl_setopt($curlHandleCURLOPT_PROXYUSERPWD$this->_proxyCredentials)
  124.         }
  125.         
  126.         // Ensure response is returned
  127.         curl_setopt($curlHandleCURLOPT_RETURNTRANSFER,  true);
  128.         
  129.         // Set post fields / raw data
  130.         // http://www.php.net/manual/en/function.curl-setopt.php#81161
  131.         if (!is_null($rawBody|| (!is_null($variables&& count($variables0))
  132.         {
  133.             if (!is_null($rawBody))
  134.             {
  135.                 unset($headers["Content-Length"]);
  136.                 $headers["Content-Length"strlen($rawBody);   
  137.             }
  138.             curl_setopt($curlHandleCURLOPT_POSTFIELDS,  is_null($rawBody$variables $rawBody);
  139.         }
  140.  
  141.         // Set Content-Type header if required
  142.         if (!isset($headers["Content-Type"])) {
  143.             $headers["Content-Type"'';
  144.         }
  145.         
  146.         // Disable Expect: 100-Continue
  147.         // http://be2.php.net/manual/en/function.curl-setopt.php#82418
  148.         $headers["Expect"'';
  149.  
  150.         // Add additional headers to cURL instance
  151.         $curlHeaders array();
  152.         foreach ($headers as $key => $value)
  153.         {
  154.             $curlHeaders[$key.': '.$value;
  155.         }
  156.         curl_setopt($curlHandleCURLOPT_HTTPHEADER,      $curlHeaders);
  157.         
  158.         // DEBUG: curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
  159.                 
  160.         // Execute request
  161.         $rawResponse curl_exec($curlHandle);
  162.         $response    null;
  163.         if ($rawResponse)
  164.         {
  165.             $response Microsoft_Http_Response::fromString($rawResponse);
  166.             // DEBUG: var_dump($url);  
  167.             // DEBUG: var_dump(curl_getinfo($curlHandle,CURLINFO_HEADER_OUT));    
  168.             // DEBUG: var_dump($rawResponse);
  169.         }
  170.         else
  171.         {
  172.             throw new Microsoft_Http_Transport_Exception('cURL error occured during request for ' $url ': ' curl_errno($curlHandle' - ' curl_error($curlHandle));
  173.         }
  174.         curl_close($curlHandle);
  175.         
  176.         return $response;
  177.     }
  178. }

Documentation generated on Thu, 26 Nov 2009 08:04:51 +0100 by phpDocumentor 1.4.3