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
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SIWECOS
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • CurlService
  • idna_convert
  • SearchResultModule
  • TinyMCE_Compressor

Exceptions

  • CurlException

Functions

  • buildCategoryArray
  • decDate
  • getJobFileName
  • getLastActialRunTime
  • getLastScheduledRunTime
  • logMessage
  • lTrimZeros
  • markLastRun
  • parseCronFile
  • parseElement
  • runJob
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: declare(strict_types=1);
  3: 
  4: /**
  5:  * Class CurlHelper.
  6:  *
  7:  *
  8:  * @link http://php.net/manual/de/function.curl-setopt.php
  9:  * @link https://support.ladesk.com/061754-How-to-make-REST-calls-in-PHP
 10:  * @link https://stackoverflow.com/questions/9802788/call-a-rest-api-in-php
 11:  */
 12: class CurlService
 13: {
 14:     const DBG = false;
 15: 
 16:     public $error;
 17: 
 18:     /**
 19:      * @param string $url
 20:      * @param array  $data
 21:      * @param array  $header
 22:      *
 23:      * @return \stdClass
 24:      * @throws CurlException
 25:      */
 26:     public function post(string $url, array $data, array $header = [])
 27:     {
 28:         // define base options
 29:         $options = [
 30:             CURLOPT_POST           => true,
 31:             CURLOPT_RETURNTRANSFER => true,
 32:             CURLOPT_URL            => $url,
 33:         ];
 34: 
 35:         // $options[CURLOPT_HEADER] = true;
 36:         $options[CURLINFO_HEADER_OUT]    = true;
 37:         $options[CURLOPT_VERBOSE]        = true;
 38:         $options[CURLOPT_FILETIME]       = true;
 39:         $options[CURLOPT_FOLLOWLOCATION] = true;
 40:         $options[CURLOPT_MAXREDIRS]      = 3;
 41:         $options[CURLOPT_SSL_VERIFYHOST] = false;
 42:         $options[CURLOPT_SSL_VERIFYPEER] = false;
 43: 
 44:         if (self::DBG) {
 45:             $additional = [
 46:                 CURLOPT_HEADER         => true,
 47:                 CURLINFO_HEADER_OUT    => true,
 48:                 CURLOPT_VERBOSE        => true,
 49:                 CURLOPT_FILETIME       => true,
 50:                 CURLOPT_FOLLOWLOCATION => true,
 51:                 CURLOPT_MAXREDIRS      => 3,
 52:                 CURLOPT_SSL_VERIFYHOST => false,
 53:                 CURLOPT_SSL_VERIFYPEER => false,
 54:             ];
 55:             $options    = array_merge($options, $additional);
 56:         }
 57: 
 58:         // add optional POST data
 59:         if (!empty($data)) {
 60:             $options[CURLOPT_POSTFIELDS] = json_encode($data);
 61:         }
 62: 
 63:         // add optional headers
 64:         if (!empty($header)) {
 65:             $options[CURLOPT_HTTPHEADER] = $header;
 66:         }
 67: 
 68:         $response = json_decode($this->_call($options));
 69: 
 70:         return $response;
 71:     }
 72: 
 73:     /**
 74:      * @param string $url
 75:      * @param array  $header
 76:      *
 77:      * @return \stdClass
 78:      * @throws CurlException
 79:      */
 80:     public function get(string $url, array $header = [])
 81:     {
 82:         // define base options
 83:         $options = [
 84:             CURLOPT_POST           => false,
 85:             CURLOPT_RETURNTRANSFER => true,
 86:             CURLOPT_URL            => $url,
 87:         ];
 88: 
 89:         // $options[CURLOPT_HEADER] = true;
 90:         $options[CURLINFO_HEADER_OUT]    = true;
 91:         $options[CURLOPT_VERBOSE]        = true;
 92:         $options[CURLOPT_FILETIME]       = true;
 93:         $options[CURLOPT_FOLLOWLOCATION] = true;
 94:         $options[CURLOPT_MAXREDIRS]      = 3;
 95:         $options[CURLOPT_SSL_VERIFYHOST] = false;
 96:         $options[CURLOPT_SSL_VERIFYPEER] = false;
 97: 
 98:         if (self::DBG) {
 99:             $additional = [
100:                 CURLOPT_HEADER         => true,
101:                 CURLINFO_HEADER_OUT    => true,
102:                 CURLOPT_VERBOSE        => true,
103:                 CURLOPT_FILETIME       => true,
104:                 CURLOPT_FOLLOWLOCATION => true,
105:                 CURLOPT_MAXREDIRS      => 3,
106:                 CURLOPT_SSL_VERIFYHOST => false,
107:                 CURLOPT_SSL_VERIFYPEER => false,
108:             ];
109:             $options    = array_merge($options, $additional);
110:         }
111: 
112:         // add optional headers
113:         if (!empty($header)) {
114:             $options[CURLOPT_HTTPHEADER] = $header;
115:         }
116: 
117:         $response = $this->_call($options);
118: 
119:         return $response;
120:     }
121: 
122:     /**
123:      * @param array $options
124:      *
125:      * @return \stdClass
126:      * @throws CurlException
127:      */
128:     private function _call(array $options)
129:     {
130:         // init curl
131:         $curl = curl_init();
132:         if (false === $curl) {
133:             throw new CurlException('failed to init curl');
134:         }
135: 
136:         // set options
137:         $succ = curl_setopt_array($curl, $options);
138:         if (false === $succ) {
139:             throw new CurlException('failed to set curl options');
140:         }
141: 
142:         // execute call
143:         $resp = curl_exec($curl);
144:         // get info
145:         $info = curl_getinfo($curl);
146: 
147:         // close curl
148:         curl_close($curl);
149: 
150:         // check errors
151:         $this->error = [
152:             'resp' => $resp,
153:             'info' => $info,
154:         ];
155:         if (self::DBG) {
156:             error_log($info);
157:         }
158: 
159:         return $resp;
160:     }
161: }
162: 
163: class CurlException extends cException
164: {
165: }
CMS CONTENIDO 4.10.1 API documentation generated by ApiGen 2.8.0