Overview

Namespaces

  • LoginRadiusSDK
    • Advance
    • Clients
    • CustomerRegistration
      • Account
      • Authentication
      • Social
    • Utility

Classes

  • LoginRadiusSDK\Advance\ConfigAPI
  • LoginRadiusSDK\Advance\WebHooksAPI
  • LoginRadiusSDK\Clients\DefaultHttpClient
  • LoginRadiusSDK\CustomerRegistration\Account\AccountAPI
  • LoginRadiusSDK\CustomerRegistration\Account\CustomObjectAPI
  • LoginRadiusSDK\CustomerRegistration\Account\CustomRegistrationDataAPI
  • LoginRadiusSDK\CustomerRegistration\Account\RoleAPI
  • LoginRadiusSDK\CustomerRegistration\Authentication\AuthCustomObjectAPI
  • LoginRadiusSDK\CustomerRegistration\Authentication\UserAPI
  • LoginRadiusSDK\CustomerRegistration\Social\AdvanceSocialLoginAPI
  • LoginRadiusSDK\CustomerRegistration\Social\SocialLoginAPI
  • LoginRadiusSDK\Utility\Functions

Interfaces

  • LoginRadiusSDK\Clients\IHttpClient

Exceptions

  • LoginRadiusSDK\LoginRadiusException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 
<?php

/**
 * @link : http://www.loginradius.com
 * @category : Clients
 * @package : DefaultHttpClient
 * @author : LoginRadius Team
 * @version : 5.0.1
 * @license : https://opensource.org/licenses/MIT
 */

namespace LoginRadiusSDK\Clients;

use LoginRadiusSDK\Utility\Functions;
use LoginRadiusSDK\LoginRadiusException;
use LoginRadiusSDK\Clients\IHttpClient;

/**
 * Class DefaultHttpClient
 *
 * Use default Curl/fsockopen to get response from LoginRadius APIs.
 *
 * @package LoginRadiusSDK\Clients
 */
class DefaultHttpClient implements IHttpClient {

    /**
     * @param $path
     * @param array $query_array
     * @param array $options
     * @return type
     * @throws \LoginRadiusSDK\LoginRadiusException
     */
    public function request($path, $query_array = array(), $options = array()) {
        $parse_url = parse_url($path);
        $request_url = '';
        if (!isset($parse_url['scheme']) || empty($parse_url['scheme'])) {
            $request_url .= API_DOMAIN;
        }

        $request_url .= $path;
        if ($query_array !== false) {
            
            if (isset($options['authentication']) && $options['authentication'] == 'secret') {
                if (!isset($options['api_request_signing']) || !$options['api_request_signing']) {
                    $options = array_merge($options, Functions::authentication(array(), $options['authentication']));
                }
                $query_array = isset($options['authentication']) ? Functions::authentication($query_array) : $query_array;
            } else if(isset($options['authentication']) && $options['authentication'] == 'secretparams') {
                $query_array = isset($options['authentication']) ? Functions::authentication($query_array, $options['authentication']) : $query_array;
            } else {
                $query_array = isset($options['authentication']) ? Functions::authentication($query_array, $options['authentication']) : $query_array;
            }
        
            $request_url .= (strpos($request_url, "?") === false) ? "?" : "&";
            $request_url .= Functions::queryBuild($query_array);
        
            if (isset($options['authentication']) && $options['authentication'] == 'secret' && isset($options['api_request_signing']) && $options['api_request_signing']) {
                $options = array_merge($options, Functions::authentication($options, 'hashsecret', $request_url));
            }
        }
    
        if (in_array('curl', get_loaded_extensions())) {
            $response = $this->curlApiMethod($request_url, $options);
        } elseif (ini_get('allow_url_fopen')) {
            $response = $this->fsockopenApiMethod($request_url, $options);
        } else {
            throw new LoginRadiusException('cURL or FSOCKOPEN is not enabled, enable cURL or FSOCKOPEN to get response from LoginRadius API.');
        }

        if (!empty($response)) {
            $result = json_decode($response);
            if (isset($result->ErrorCode) && !empty($result->ErrorCode)) {
                throw new LoginRadiusException($result->Message, $result);
            }
        }
        return $response;
    }

    /**
     * Access LoginRadius API server by curl method
     *
     * @param type $request_url
     * @param type $options
     * @return type
     */
    private function curlApiMethod($request_url, $options = array()) {
        $ssl_verify = isset($options['ssl_verify']) ? $options['ssl_verify'] : false;
        $method = isset($options['method']) ? strtoupper($options['method']) : 'GET';
        $data = isset($options['post_data']) ? $options['post_data'] : array();
        $content_type = isset($options['content_type']) ? trim($options['content_type']) : 'x-www-form-urlencoded';
        $auth_access_token = isset($options['access-token']) ? trim($options['access-token']) : '';
        $sott_header_content = isset($options['X-LoginRadius-Sott']) ? trim($options['X-LoginRadius-Sott']) : '';
        $secret_header_content = isset($options['X-LoginRadius-ApiSecret']) ? trim($options['X-LoginRadius-ApiSecret']) : '';
        $expiry_time = isset($options['X-Request-Expires']) ? trim($options['X-Request-Expires']) : '';
        $digest = isset($options['digest']) ? trim($options['digest']) : '';

        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $request_url);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($curl_handle, CURLOPT_TIMEOUT, 50);
        curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
        $optionsArray = array('Content-type: application/' . $content_type);
        if ($auth_access_token != '') {
            $optionsArray[] = 'Authorization:' . $auth_access_token;
        }
        if ($sott_header_content != '') {
            $optionsArray[] = 'X-LoginRadius-Sott:' . $sott_header_content;
        }
        if ($secret_header_content != '') {
            $optionsArray[] = 'X-LoginRadius-ApiSecret:' . $secret_header_content;
        }
        if ($expiry_time != '') {
            $optionsArray[] = 'X-Request-Expires:' . $expiry_time;
        }
        if ($digest != '') {
            $optionsArray[] = 'digest:' . $digest;
        }
        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $optionsArray);

        if (isset($options['proxy']) && $options['proxy']['host'] != '' && $options['proxy']['port'] != '') {
            curl_setopt($curl_handle, CURLOPT_PROXY, $options['proxy']['protocol'] . '://' . $options['proxy']['user'] . ':' . $options['proxy']['password'] . '@' . $options['proxy']['host'] . ':' . $options['proxy']['port']);
        }

        if (!empty($data) || $data === true) {
            if (($content_type == 'json') && (is_array($data) || is_object($data))) {
                $data = json_encode($data);
            }

            curl_setopt($curl_handle, CURLOPT_POSTFIELDS, (($content_type == 'json') ? $data : Functions::queryBuild($data)));

            if (in_array($method, array('POST', 'PUT', 'DELETE'))) {
                curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $method);
            }
        }
        curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

        $json_response = curl_exec($curl_handle);
        if (curl_error($curl_handle)) {
            $json_response = curl_error($curl_handle);
        }

        curl_close($curl_handle);
        return $json_response;
    }

    /**
     * Access LoginRadius API server by fsockopen method
     *
     * @param type $request_url
     * @param type $options
     * @return type
     */
    private function fsockopenApiMethod($request_url, $options = array()) {
        $ssl_verify = isset($options['ssl_verify']) ? $options['ssl_verify'] : false;
        $method = isset($options['method']) ? strtoupper($options['method']) : 'GET';
        $data = isset($options['post_data']) ? $options['post_data'] : array();
        $content_type = isset($options['content_type']) ? $options['content_type'] : 'form_params';
        $auth_access_token = isset($options['access-token']) ? trim($options['access-token']) : '';
        $sott_header_content = isset($options['X-LoginRadius-Sott']) ? trim($options['X-LoginRadius-Sott']) : '';
        $secret_header_content = isset($options['X-LoginRadius-ApiSecret']) ? trim($options['X-LoginRadius-ApiSecret']) : '';
        $expiry_time = isset($options['X-Request-Expires']) ? trim($options['X-Request-Expires']) : '';
        $digest = isset($options['digest']) ? trim($options['digest']) : '';

        $optionsArray = array('http' =>
            array(
                'method' => strtoupper($method),
                'timeout' => 50,
                'ignore_errors' => true,
                'header' => 'Content-Type: application/' . $content_type
            ),
            "ssl" => array(
                "verify_peer" => $ssl_verify
            )
        );
        if (!empty($data) || $data === true) {
            if (($content_type == 'json') && (is_array($data) || is_object($data))) {
                $data = json_encode($data);
            }
            $optionsArray['http']['header'] .= "\r\n" . 'Content-Length:' . (($data === true) ? '0' : strlen($data));
            $optionsArray['http']['content'] = (($content_type == 'json') ? $data : Functions::queryBuild($data));
        }
        if ($auth_access_token != '') {
            $optionsArray['http']['header'] .= "\r\n" . 'Authorization: ' . $auth_access_token;
        }
        if ($sott_header_content != '') {
            $optionsArray['http']['header'] .= "\r\n" . 'X-LoginRadius-Sott: ' . $sott_header_content;
        }
        if ($secret_header_content != '') {
            $optionsArray['http']['header'] .= "\r\n" . 'X-LoginRadius-ApiSecret: ' . $secret_header_content;
        }
        if ($expiry_time != '') {
            $optionsArray['http']['header'] .= "\r\n" . 'X-Request-Expires: ' . $expiry_time;
        }
        if ($digest != '') {
            $optionsArray['http']['header'] .= "\r\n" . 'digest: ' . $digest;
        }

        $context = stream_context_create($optionsArray);
        $json_response = file_get_contents($request_url, false, $context);
        if (!$json_response) {
            throw new LoginRadiusException('file_get_contents error');
        }
        return $json_response;
    }

}
API documentation generated by ApiGen