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