Last active
September 27, 2018 13:56
-
-
Save danganf/9b801ff15a47a13b6b01cb56adf51228 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::any('/{method}/{destiny}/{path?}', 'RequestController@action' )->name('factory'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace IntercaseDefault\MyClass; | |
class Curl{ | |
private $timeout = 15; | |
private $connectionTimeout = 10; | |
public function __construct () { | |
// | |
} | |
protected function setTimeOut($time) {$this->timeout = $time;} | |
protected function setconnectionTimeout($time){$this->connectionTimeout = $time;} | |
public function send( $url, array $options = [] ){ | |
$ch = curl_init(); | |
if (count($options) > 0) { | |
if (!empty($options['timeout'])) | |
$this->timeout = $options['timeout']; | |
if (!empty($options['connectionTimeout'])) | |
$this->connectionTimeout = $options['connectionTimeout']; | |
if (!empty($options['json'])) { | |
if (!empty($options['header'])) { | |
$options['header'][] = 'Content-Type: application/json'; | |
$options['header'][] = 'Content-Length: ' . strlen($options['data']); | |
} | |
} | |
if (!empty($options['header'])) { | |
if (is_array($options['header'])) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); | |
} else { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array($options['header'])); | |
} | |
} | |
if (!empty($options['method'])) | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $options['method']); | |
if (!empty($options['post']) && !empty($options['post'])) | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
if ( !empty($options['data']) && in_array( array_get( $options, 'method', NULL ), ['POST','PUT','PATCH','DELETE'] ) ) | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $options['data']); | |
} | |
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout); | |
//curl_setopt($ch, CURLOPT_REFERER, ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null ) ); | |
if( array_has( $options, 'returnHeaders' ) ) { | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
} | |
$result = curl_exec($ch); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if( array_has( $options, 'returnHeaders' ) ) { | |
$info = curl_getinfo($ch); | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$textHeaders = substr($result, 0, $header_size); | |
$result = substr($result, $header_size); | |
// buscando dentro dos header retornados o que foi selecionado | |
$headersReturn = []; | |
$data = explode("\n",$textHeaders); | |
array_shift($data); | |
foreach($data as $part){ | |
$middle = explode(":",$part); | |
if( in_array( trim($middle[0]), array_get( $options, 'returnHeaders' ) ) ) { | |
$headersReturn[trim($middle[0])] = trim($middle[1]); | |
} | |
} | |
$arrayResult = [ 'RESULT' => $result, 'HTTP_CODE' => $httpcode, 'HEADER_RETURN' => $headersReturn ]; | |
} else { | |
$arrayResult = [ 'RESULT' => $result, 'HTTP_CODE' => $httpcode ]; | |
} | |
curl_close($ch); | |
return $arrayResult; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\MyClass; | |
use App\MyClass\Contracts\Requests; | |
use Illuminate\Http\Request; | |
use IntercaseDefault\MyClass\Curl; | |
class FactoryApis extends Requests { | |
private $options, $request, $method, $paramsGet=[]; | |
public function __construct(Curl $curl) | |
{ | |
parent::__construct($curl); | |
if( !empty( $this->getHeader() ) ){ | |
$this->options = [ 'header' => $this->getHeader() ]; | |
} else { | |
exit(''); | |
} | |
} | |
public function setPaginate(){ | |
$this->isPaginate = true; | |
$this->setHeader('returnHeaders',$this->nameKeyPaginator); | |
$this->options = ['header' => $this->getHeader() ]; | |
} | |
public function setFilters($filterArray){ | |
foreach ( $filterArray as $key => $value ){ | |
$this->setParamsGet( $key, $value ); | |
} | |
} | |
public function setParamsGet($param,$value){ | |
$this->paramsGet[$param] = htmlentities(urlencode($value)); | |
} | |
public function setRequest(Request $request){ | |
$this->request = $request; | |
return $this; | |
} | |
public function __call($method, $args) { | |
$this->method = strtoupper($method); | |
$this->processDestiny($args[0]); | |
unset($args[0]); | |
return $this->{'Call'.ucfirst(strtolower($method))}($args); | |
} | |
public function CallGet($path=null) { | |
$return = $this->send($this->processPath($path), $this->processOptions() ); | |
if ( !is_array( $return ) ) { | |
$return = []; | |
} | |
return $return; | |
} | |
public function CallPost($path=null) { | |
return $this->CallPut($path); | |
} | |
public function CallPut($path=null) { | |
$return = $this->send($this->processPath($path), $this->processOptions() ); | |
if ( !is_array( $return ) ) { | |
$return = []; | |
} | |
return $return; | |
} | |
public function CallDelete($path=null) { | |
$return = $this->send($this->processPath($path), $this->processOptions() ); | |
if ( !is_array( $return ) ) { | |
$return = []; | |
} | |
return $return; | |
} | |
private function processPath($path=null){ | |
$queryString = ''; | |
if( $this->method == 'GET' && ( $this->request instanceof Request || !empty( $this->paramsGet ) ) ){ | |
if( empty( $this->paramsGet ) ) { | |
$values = $this->request->all(); | |
array_pull($values, 'api_key'); | |
array_pull($values, 'store_id'); | |
array_pull($values, 'company_id'); | |
} else { | |
$values = $this->paramsGet; | |
$this->paramsGet = []; | |
} | |
$queryString = implodeArrayQueryString( $values ); | |
$queryString = (empty($queryString) ? '' : '?'.$queryString); | |
} | |
$path = is_array( $path ) ? current( $path ) : $path; | |
$path = ( empty( $path ) ? '' : $path ); | |
return $this->pathUrl.'/'.str_replace(['@','/'],'/', $path).$queryString; | |
} | |
private function processOptions(){ | |
if( $this->method !== 'GET' ){ | |
$this->options['method'] = $this->method; | |
if( !empty($this->request->all()) ) { | |
$this->options['data'] = json_encode($this->request->all()); | |
$this->options['json'] = true; | |
} | |
} | |
return $this->options; | |
} | |
private function processDestiny($destiny){ | |
$this->table = $destiny; | |
$this->setPathUrl(config('app.url_pdv')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\MyClass\Contracts; | |
use IntercaseDefault\MyClass\Curl; | |
abstract class Requests { | |
protected $curl, $pathUrl, $table, $nameKeyPaginator = 'x-paginator-df', $isPaginate = false; | |
private $header = [], $returnHeader = null; | |
public function __construct(Curl $curl) | |
{ | |
$this->curl = $curl; | |
$this->initHeader(); | |
} | |
public function setPathUrl($url){ | |
$this->pathUrl = $url . '/' . $this->table; | |
} | |
protected function setHeaderDF(){ | |
$this->setHeader('X-DreamFactory-API-Key', config('app.api_key_df')); | |
} | |
public function send($host,$options){ | |
if( $this->isPaginate ){ | |
$options['returnHeaders'] = [$this->nameKeyPaginator]; | |
} | |
$return = $this->curl->send($host, $options); | |
$result = null; | |
if (is_array($return)) { | |
$result = json_decode( $return['RESULT'], true ); | |
$error = false; | |
if( is_array( $result ) ) { | |
if ( key_exists('error', $result) ) { | |
$error = true; | |
} else if ( array_has( $result, 'resource' ) ) { | |
$result = current($result); | |
} | |
} else if( empty( $result ) ){ | |
$result = $return['RESULT']; | |
$error = true; | |
} | |
if($error){ | |
\LogDebug::error($return['RESULT']); | |
} else if ( array_get( $return, 'HEADER_RETURN' ) ){ | |
$this->returnHeader = $return['HEADER_RETURN']; | |
} | |
} | |
return $result; | |
} | |
public function getHeaderReturn(){ | |
$return = $this->returnHeader; | |
$this->returnHeader = null; | |
return $return; | |
} | |
private function initHeader(){ | |
$sessionValues = SessionOpen('get'); | |
$tokenLabel = array_get( $sessionValues, 'token_label' ); | |
$tokenValue = array_get( $sessionValues, 'token_value' ); | |
if( !empty( $tokenLabel ) && !empty( $tokenValue ) ){ | |
$this->setHeader($tokenLabel, $tokenValue); | |
} | |
} | |
public function getHeader(){ | |
return $this->header; | |
} | |
public function setHeader($label, $value){ | |
$this->header[] = "$label: $value"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment