[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Copyright 2014 Google Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * Curl based implementation of Google_IO. 20 * 21 * @author Stuart Langley <[email protected]> 22 */ 23 24 require_once 'Google/IO/Abstract.php'; 25 26 class Google_IO_Curl extends Google_IO_Abstract 27 { 28 // cURL hex representation of version 7.30.0 29 const NO_QUIRK_VERSION = 0x071E00; 30 31 private $options = array(); 32 /** 33 * Execute an HTTP Request 34 * 35 * @param Google_HttpRequest $request the http request to be executed 36 * @return Google_HttpRequest http request with the response http code, 37 * response headers and response body filled in 38 * @throws Google_IO_Exception on curl or IO error 39 */ 40 public function executeRequest(Google_Http_Request $request) 41 { 42 $curl = curl_init(); 43 44 if ($request->getPostBody()) { 45 curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody()); 46 } 47 48 $requestHeaders = $request->getRequestHeaders(); 49 if ($requestHeaders && is_array($requestHeaders)) { 50 $curlHeaders = array(); 51 foreach ($requestHeaders as $k => $v) { 52 $curlHeaders[] = "$k: $v"; 53 } 54 curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); 55 } 56 57 curl_setopt($curl, CURLOPT_URL, $request->getUrl()); 58 59 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod()); 60 curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); 61 62 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 63 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 64 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 65 curl_setopt($curl, CURLOPT_HEADER, true); 66 67 if ($request->canGzip()) { 68 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 69 } 70 71 foreach ($this->options as $key => $var) { 72 curl_setopt($curl, $key, $var); 73 } 74 75 if (!isset($this->options[CURLOPT_CAINFO])) { 76 curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem'); 77 } 78 79 $response = curl_exec($curl); 80 if ($response === false) { 81 throw new Google_IO_Exception(curl_error($curl)); 82 } 83 $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 84 85 list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize); 86 87 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 88 89 return array($responseBody, $responseHeaders, $responseCode); 90 } 91 92 /** 93 * Set options that update the transport implementation's behavior. 94 * @param $options 95 */ 96 public function setOptions($options) 97 { 98 $this->options = $options + $this->options; 99 } 100 101 /** 102 * Set the maximum request time in seconds. 103 * @param $timeout in seconds 104 */ 105 public function setTimeout($timeout) 106 { 107 // Since this timeout is really for putting a bound on the time 108 // we'll set them both to the same. If you need to specify a longer 109 // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to 110 // do is use the setOptions method for the values individually. 111 $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout; 112 $this->options[CURLOPT_TIMEOUT] = $timeout; 113 } 114 115 /** 116 * Get the maximum request time in seconds. 117 * @return timeout in seconds 118 */ 119 public function getTimeout() 120 { 121 return $this->options[CURLOPT_TIMEOUT]; 122 } 123 124 /** 125 * Test for the presence of a cURL header processing bug 126 * 127 * {@inheritDoc} 128 * 129 * @return boolean 130 */ 131 protected function needsQuirk() 132 { 133 $ver = curl_version(); 134 $versionNum = $ver['version_number']; 135 return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION; 136 } 137 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |