Skip to content
Snippets Groups Projects
CurlModel.php 4.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * Copyright Maarch since 2008 under licence GPLv3.
     * See LICENCE.txt file at the root folder for more details.
     * This file is part of Maarch software.
     *
     */
    
    /**
     * @brief Curl Model
     * @author dev@maarch.org
     */
    
    
    Damien's avatar
    Damien committed
    namespace SrcCore\models;
    
    class CurlModel
    {
        public static function exec(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['curlCallId']);
            ValidatorModel::stringType($aArgs, ['curlCallId']);
            ValidatorModel::arrayType($aArgs, ['bodyData']);
    
            ValidatorModel::boolType($aArgs, ['noAuth']);
    
    
            $curlConfig = CurlModel::getConfigByCallId(['curlCallId' => $aArgs['curlCallId']]);
            if (empty($curlConfig)) {
                return [];
            }
    
            $opts = [
                CURLOPT_URL => $curlConfig['url'],
                CURLOPT_HTTPHEADER => [
                    'accept:application/json',
                    'content-type:application/json'
                ],
                CURLOPT_RETURNTRANSFER => true,
            ];
    
            if (empty($aArgs['noAuth'])) {
                $opts[CURLOPT_HTTPHEADER][] = 'Authorization: Basic ' . base64_encode($curlConfig['user']. ':' .$curlConfig['password']);
            }
    
    
            if ($curlConfig['method'] == 'POST' || $curlConfig['method'] == 'PUT') {
                if (!empty($curlConfig['data'])) {
                    $data = [];
                    foreach ($curlConfig['data'] as $key => $value) {
                        $data[$key] = $aArgs['bodyData'][$value];
                    }
                    $opts[CURLOPT_POSTFIELDS] = json_encode($data);
                } else {
                    $opts[CURLOPT_POSTFIELDS] = json_encode($aArgs['bodyData']);
                }
            }
            if ($curlConfig['method'] == 'POST') {
                $opts[CURLOPT_POST] = true;
            } elseif ($curlConfig['method'] == 'PUT' || $curlConfig['method'] == 'DELETE') {
                $opts[CURLOPT_CUSTOMREQUEST] = $curlConfig['method'];
            }
    
            $curl = curl_init();
            curl_setopt_array($curl, $opts);
            $rawResponse = curl_exec($curl);
    
            return json_decode($rawResponse, true);
        }
    
    
    Damien's avatar
    Damien committed
        public static function execSOAP(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['xmlPostString', 'url', 'soapAction']);
            ValidatorModel::stringType($aArgs, ['xmlPostString', 'url', 'soapAction']);
    
    Damien's avatar
    Damien committed
            ValidatorModel::arrayType($aArgs, ['options']);
    
    Damien's avatar
    Damien committed
    
            $opts = [
                CURLOPT_URL             => $aArgs['url'],
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_POST            => true,
                CURLOPT_POSTFIELDS      => $aArgs['xmlPostString'],
                CURLOPT_HTTPHEADER      => [
                    'content-type:text/xml;charset="utf-8"',
                    'accept:text/xml',
                    'Cache-Control: no-cache',
                    'Pragma: no-cache',
                    'Content-length: ' . strlen($aArgs['xmlPostString']),
                    "SOAPAction: \"{$aArgs['soapAction']}\""
                ]
            ];
    
    
    Damien's avatar
    Damien committed
            if (!empty($aArgs['options'])) {
                $opts = array_merge($opts, $aArgs['options']);
            }
    
    
    Damien's avatar
    Damien committed
            $curl = curl_init();
            curl_setopt_array($curl, $opts);
            $rawResponse = curl_exec($curl);
    
            return simplexml_load_string($rawResponse);
        }
    
    
        public static function getConfigByCallId(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['curlCallId']);
            ValidatorModel::stringType($aArgs, ['curlCallId']);
    
            $curlConfig = [];
    
            $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/curlCall.xml']);
            if ($loadedXml) {
    
                $curlConfig['user']     = (string)$loadedXml->user;
                $curlConfig['password'] = (string)$loadedXml->password;
    
                foreach ($loadedXml->call as $call) {
                    if ((string)$call->id == $aArgs['curlCallId']) {
                        $curlConfig['url']      = (string)$call->url;
                        $curlConfig['method']   = strtoupper((string)$call->method);
                        if (!empty($call->data)) {
                            $curlConfig['data'] = [];
                            foreach ($call->data as $data) {
                                $curlConfig['data'][(string)$data->key] = (string)$data->value;
                            }
                        }
                    }
                }
            }
    
            return $curlConfig;
        }
    }