Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file AdiantiHttpClient.php

Documentation is available at AdiantiHttpClient.php

  1. <?php
  2. namespace Adianti\Http;
  3.  
  4. use Adianti\Core\AdiantiCoreApplication;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Exception;
  7.  
  8. /**
  9.  * Basic HTTP Client request
  10.  *
  11.  * @version    7.4
  12.  * @package    core
  13.  * @author     Pablo Dall'Oglio
  14.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  15.  * @license    http://www.adianti.com.br/framework-license
  16.  */
  17. {
  18.     /**
  19.      * Execute a HTTP request
  20.      *
  21.      * @param $url URL
  22.      * @param $method method type (GET,PUT,DELETE,POST)
  23.      * @param $params request body
  24.      */
  25.     public static function request($url$method 'POST'$params []$authorization null)
  26.     {
  27.         $ch curl_init();
  28.         
  29.         if ($method == 'POST' || $method == 'PUT')
  30.         {
  31.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($params));
  32.             curl_setopt($chCURLOPT_POSTtrue);
  33.      
  34.         }
  35.         else if ( ($method == 'GET' || $method == 'DELETE'&& $params)
  36.         {
  37.             $url .= '?'.http_build_query($params);
  38.         }
  39.        
  40.         $defaults array(
  41.             CURLOPT_URL => $url,
  42.             CURLOPT_HEADER => false,
  43.             CURLOPT_CUSTOMREQUEST => $method,
  44.             CURLOPT_RETURNTRANSFER => true,
  45.             CURLOPT_SSL_VERIFYHOST => false,
  46.             CURLOPT_SSL_VERIFYPEER => false,
  47.             CURLOPT_CONNECTTIMEOUT => 10
  48.         );
  49.         
  50.         if (!empty($authorization))
  51.         {
  52.             $defaults[CURLOPT_HTTPHEADER['Authorization: '$authorization];
  53.         }
  54.         
  55.         curl_setopt_array($ch$defaults);
  56.         $output curl_exec ($ch);
  57.         
  58.         if ($output === false)
  59.         {
  60.             throw new Exceptioncurl_error($ch) );
  61.         }
  62.         
  63.         curl_close ($ch);
  64.         
  65.         $return = (array) json_decode($output);
  66.         
  67.         if (json_last_error(!== JSON_ERROR_NONE)
  68.         {
  69.             throw new Exception(AdiantiCoreTranslator::translate('Return is not a valid JSON. Check the URL'' ' AdiantiCoreApplication::getDebugMode($output '') );
  70.         }
  71.         
  72.         if (!empty($return['status']&& $return['status'== 'error'{
  73.             throw new Exception(!empty($return['data']$return['data'$return['message']);
  74.         }
  75.         
  76.         if (!empty($return['error'])) {
  77.             throw new Exception($return['error']['message']);
  78.         }
  79.         
  80.         if (!empty($return['errors'])) {
  81.             throw new Exception($return['errors']['message']);
  82.         }
  83.         
  84.         if (!empty($return['data']))
  85.         {
  86.             return $return['data'];
  87.         }
  88.         
  89.         return $return;
  90.     }
  91. }