Skip to content

Instantly share code, notes, and snippets.

@patidardhaval
Created March 10, 2018 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patidardhaval/ac68a37a0d9a2a2d2780d54aab088ca8 to your computer and use it in GitHub Desktop.
Save patidardhaval/ac68a37a0d9a2a2d2780d54aab088ca8 to your computer and use it in GitHub Desktop.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
<?php
$url = "http://google.com";
function __autoload($classname)
{
$filename = "" . $classname . ".php";
include_once $filename;
}
function json_user($q_para, $para, $debug = 0, $method = 'POST')
{
global $url;
$url = $url . $q_para;
$config['method'] = $method;
$config['url'] = $url;
$config['eco'] = $debug;
if (isset($_SESSION['dim_token'])) {
$config['token'] = $_SESSION['dim_token'];
} else {
$config['token'] = '';
}
$obj = new RestClientAPI($config);
$data = $obj->request($para);
if(isset($data))
{
$json = json_decode($data);
}
return $data ? $data : 0;
}
$method = $_SERVER['REQUEST_METHOD'];
$params = json_decode(file_get_contents('php://input'),true);
#split the path by '/'
$rurl = $_SERVER['REQUEST_URI'];
$q_para = parse_url($rurl, PHP_URL_PATH);
echo json_user($q_para, $params, $debug = 0, $method);
<?php
class RestClientAPI
{
private $url = "";
private $jwt = "";
private $method = "POST";
private $eco = 0;
public function __construct(array $config)
{
$this->method = $config['method'];
$this->url = $config['url'];
$this->eco = $config['eco'];
$this->jwt = $config['token'];
}
public function request($params)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "gzip",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $this->method,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_HTTPHEADER => array(
"authorization: bearer ".$this->jwt,
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
),
));
if ($this->eco === 1) {
var_dump(curl_getinfo($curl));
}
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$errarr = array(
"error" =>$err
);
echo json_encode($errarr);
} else {
return $result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment