Created
September 26, 2018 08:39
-
-
Save SebbeJohansson/26fe98b32757ffe3de2d5f2546fe91a4 to your computer and use it in GitHub Desktop.
clCurl - Curl wrapper.
This file contains 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 Apex\Core; | |
class clCURLold { | |
public $oRequest; | |
public $iLastHTTPStatus; | |
public $aOptions = array(); | |
public $aHeaders = array(); | |
public $aPostFields = array(); | |
public function __construct() { | |
$this->oRequest = curl_init(); | |
//curl_setopt($this->oRequest, CURLOPT_HEADER, true); | |
curl_setopt($this->oRequest, CURLOPT_RETURNTRANSFER, true); | |
} | |
public function __destruct() { | |
curl_close($this->oRequest); | |
} | |
public function close(){ | |
$this->__destruct(); | |
} | |
public function prepareCURL($sUrl, $sMode, $aOptions = array(), $aHeaders = array(), $aPostFields = array()){ | |
curl_setopt($this->oRequest, CURLOPT_URL, $sUrl); | |
curl_setopt($this->oRequest, CURLOPT_CUSTOMREQUEST, $sMode); | |
$this->aOptions = array_merge($this->aOptions, $aOptions); | |
foreach($this->aOptions as $sOption=>$sValue){ | |
$sOption = strtoupper($sOption); | |
curl_setopt($this->oRequest, constant("CURLOPT_$sOption"), $sValue); | |
} | |
$this->aHeaders = array_merge($this->aHeaders, $aHeaders); | |
$aLocalHeaders = array(); | |
foreach($aHeaders as $sHeader => $sValue){ | |
$aLocalHeaders[] = "$sHeader:$sValue"; | |
} | |
curl_setopt($this->oRequest, CURLOPT_HTTPHEADER, $aLocalHeaders); | |
$this->aPostFields = array_merge($this->aPostFields, $aPostFields); | |
curl_setopt($this->oRequest, CURLOPT_POSTFIELDS, $this->aPostFields); | |
} | |
public function executeCURL(){ | |
$content = curl_exec($this->oRequest); | |
$this->iLastHTTPStatus = curl_getinfo($this->oRequest, CURLINFO_HTTP_CODE); | |
return $content; | |
} | |
public function decodeJson($sContent = ""){ | |
return json_decode($sContent, true); | |
} | |
public function get($url, $variables = array()){ | |
$this->prepareCURL($url, "GET", array(), array(), $variables); | |
return $this->executeCURL(); | |
} | |
public function post($url, $variables = array()){ | |
$this->prepareCURL($url, "POST", array(), array(), $variables); | |
return $this->executeCURL(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment