Overview

Packages

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