Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cHttpRequest
  • cHttpRequestCurl
  • cHttpRequestSocket
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains an implementation of HttpRequest using curl
  5:  *
  6:  * @package Core
  7:  * @subpackage Core
  8:  * @author Mischa Holz
  9:  * @copyright four for business AG <www.4fb.de>
 10:  * @license http://www.contenido.org/license/LIZENZ.txt
 11:  * @link http://www.4fb.de
 12:  * @link http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * Curl implementation of HttpRequest.
 19:  *
 20:  * @package Core
 21:  * @subpackage Core
 22:  */
 23: class cHttpRequestCurl extends cHttpRequest {
 24: 
 25:     /**
 26:      * The curl instance.
 27:      *
 28:      * @var $curl resource
 29:      */
 30:     protected $curl;
 31: 
 32:     /**
 33:      * Array for the post parameters.
 34:      *
 35:      * @var array
 36:      */
 37:     protected $postArray;
 38: 
 39:     /**
 40:      * Array for the get parameters.
 41:      *
 42:      * @var array
 43:      */
 44:     protected $getArray;
 45: 
 46:     /**
 47:      * Array for the HTTP-headers.
 48:      *
 49:      * @var array
 50:      */
 51:     protected $headerArray;
 52: 
 53:     /**
 54:      * Request URL.
 55:      *
 56:      * @var string
 57:      */
 58:     protected $url;
 59: 
 60:     /**
 61:      * Constructor to create an instance of this class.
 62:      *
 63:      * @see cHttpRequest::__construct()
 64:      * @see cHttpRequest::getHttpRequest()
 65:      * @param string $url [optional]
 66:      *         URL for the request
 67:      */
 68:     public function __construct($url = '') {
 69:         $this->curl = curl_init(($url == '') ? NULL : $url);
 70:         $this->setURL($url);
 71:     }
 72: 
 73:     /**
 74:      * Set the GET parameters.
 75:      *
 76:      * @see cHttpRequest::setGetParams()
 77:      * @param array $array
 78:      *         associative array containing keys and values of the GET parameters
 79:      * @return cHttpRequest
 80:      */
 81:     public function setGetParams($array) {
 82:         $this->getArray = $array;
 83: 
 84:         return $this;
 85:     }
 86: 
 87:     /**
 88:      * Set the POST parameters.
 89:      *
 90:      * @see cHttpRequest::setPostParams()
 91:      * @param array $array
 92:      *         associative array containing keys and values of the POST parameters
 93:      * @return cHttpRequest
 94:      */
 95:     public function setPostParams($array) {
 96:         $this->postArray = $array;
 97: 
 98:         return $this;
 99:     }
100: 
101:     /**
102:      * Set the HTTP headers.
103:      *
104:      * @see cHttpRequest::setHeaders()
105:      * @param array $array
106:      *         associative array containing the HTTP headers
107:      * @return cHttpRequest
108:      */
109:     public function setHeaders($array) {
110:         $this->headerArray = $array;
111: 
112:         return $this;
113:     }
114: 
115:     /**
116:      * Set the request URL.
117:      *
118:      * @see cHttpRequest::setURL()
119:      * @param string $url
120:      *         the URL
121:      * @return cHttpRequest
122:      */
123:     public function setURL($url) {
124:         $this->url = $url;
125: 
126:         return $this;
127:     }
128: 
129:     /**
130:      * Inserts the POST array into the headers and encodes it.
131:      */
132:     protected function preparePostRequest() {
133:         if (is_array($this->postArray)) {
134:             $this->setOpt(CURLOPT_POST, 1);
135:             $this->setOpt(CURLOPT_POSTFIELDS, $this->postArray);
136:         }
137:     }
138: 
139:     /**
140:      * Appends the GET array to the URL.
141:      */
142:     protected function prepareGetRequest() {
143:         if (is_array($this->getArray)) {
144:             if (!cString::contains($this->url, '?')) {
145:                 $this->url .= "?";
146:             } else {
147:                 $this->url .= '&';
148:             }
149:             foreach ($this->getArray as $key => $value) {
150:                 $this->url .= urlencode($key) . '=' . urlencode($value) . '&';
151:             }
152:             $this->url = substr($this->url, 0, strlen($this->url) - 1);
153:         }
154:         $this->setOpt(CURLOPT_URL, $this->url);
155:     }
156: 
157:     /**
158:      * Reads all the custom headers and add them to the header string.
159:      */
160:     protected function prepareHeaders() {
161:         $curlHeaderArray = array();
162:         if (!is_array($this->headerArray)) {
163:             return;
164:         }
165:         foreach ($this->headerArray as $key => $value) {
166:             $headerString = '';
167:             if (is_array($value)) {
168:                 $headerString .= $value[0] . ': ' . $value[1];
169:             } else {
170:                 $headerString .= $key . ': ' . $value;
171:             }
172:             array_push($curlHeaderArray, $headerString);
173:         }
174: 
175:         $this->setOpt(CURLOPT_HTTPHEADER, $curlHeaderArray);
176:     }
177: 
178:     /**
179:      * Send the request to the server.
180:      *
181:      * @param bool $return
182:      *         Wether the function should return the servers response
183:      * @param string $method
184:      *         GET or POST
185:      * @param bool $returnHeaders
186:      *         Wether the headers should be included in the response
187:      * @return string|bool
188:      */
189:     protected function sendRequest($return, $method, $returnHeaders) {
190:         $this->setOpt(CURLOPT_RETURNTRANSFER, true);
191:         $this->setOpt(CURLOPT_HEADER, true);
192:         $this->setOpt(CURLOPT_URL, $this->url);
193: 
194:         $this->prepareHeaders();
195:         $this->prepareGetRequest();
196:         if ($method = 'POST') {
197:             $this->preparePostRequest();
198:         }
199: 
200:         $string = curl_exec($this->curl);
201: 
202:         if ($return) {
203:             if (!$returnHeaders) {
204:                 $string = substr(cString::strstr($string, "\r\n\r\n"), strlen("\r\n\r\n"));
205:             }
206:             return $string;
207:         } else {
208:             return strpos(cString::strstr($string, "\r\n", true), '200') !== false || strpos(cString::strstr($string, "\r\n", true), '100') !== false;
209:         }
210:     }
211: 
212:     /**
213:      * Perform the request using POST.
214:      *
215:      * @see cHttpRequest::postRequest()
216:      * @param bool $return [optional]
217:      *         If true, response of the server gets returned as string
218:      * @param bool $returnHeaders [optional]
219:      *         If true, headers will be included in the response
220:      * @return string|bool
221:      *         False on error, response otherwise
222:      */
223:     public function postRequest($return = true, $returnHeaders = false) {
224:         return $this->sendRequest($return, 'POST', $returnHeaders);
225:     }
226: 
227:     /**
228:      * Perform the request using GET.
229:      *
230:      * @see cHttpRequest::getRequest()
231:      * @param bool $return [optional]
232:      *         If true, response of the server gets returned as string
233:      * @param bool $returnHeaders [optional]
234:      *         If true, headers will be included in the response
235:      * @return string|bool
236:      *         False on error, response otherwise
237:      */
238:     public function getRequest($return = true, $returnHeaders = false) {
239:         return $this->sendRequest($return, 'GET', $returnHeaders);
240:     }
241: 
242:     /**
243:      * Perform the request using POST AND append all GET parameters.
244:      *
245:      * @see cHttpRequest::request()
246:      * @param bool $return [optional]
247:      *         If true, response of the server gets returned as string
248:      * @param bool $returnHeaders [optional]
249:      *         If true, headers will be included in the response
250:      * @return string|bool
251:      *         False on error, response otherwise
252:      */
253:     public function request($return = true, $returnHeaders = false) {
254:         return $this->sendRequest($return, 'POST', $returnHeaders);
255:     }
256: 
257:     /**
258:      * Sets CURL options.
259:      *
260:      * @see curl_setopt()
261:      * @param int $curlOpt
262:      *         One of the CURLOPT constants
263:      * @param mixed $value
264:      *         Value for the option
265:      * @return cHttpRequest
266:      */
267:     public function setOpt($curlOpt, $value) {
268:         curl_setopt($this->curl, $curlOpt, $value);
269: 
270:         return $this;
271:     }
272: 
273:     /**
274:      * Returns the curl reference.
275:      *
276:      * @return resource
277:      */
278:     public function getCurl() {
279:         return $this->curl;
280:     }
281: }
282: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0