Last active
November 21, 2018 00:22
-
-
Save veeeeeeeeeee/2e5688eeb357c1dc6233b715403b0003 to your computer and use it in GitHub Desktop.
PHP snippets
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
<? | |
public function curl($method, $id, $custom, $body) { | |
ob_start(); | |
$curl = curl_init(); | |
if ($id) $$url .= $id; | |
if ($custom) $url .= $custom; | |
$headers = [ | |
'Content-Type: application/json', | |
]; | |
$body = json_encode($body); | |
$opts = [ | |
CURLOPT_URL => $url, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_HEADER => 1, | |
CURLOPT_SSL_VERIFYPEER => 0, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FOLLOWLOCATION => 1, | |
CURLOPT_HTTPHEADER => $headers, | |
CURLINFO_HEADER_OUT => true | |
]; | |
if ($method === 'POST') { | |
$opts[CURLOPT_POST] = 1; | |
} | |
else if ($method !== 'GET') { | |
$opts[CURLOPT_CUSTOMREQUEST] = $method; | |
} | |
if ($method !== 'GET') { | |
$opts[CURLOPT_POSTFIELDS] = $body; | |
} | |
curl_setopt_array($curl, $opts); | |
$result = curl_exec($curl); | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
ob_end_flush(); | |
$resp = explode("\r\n\r\n", $result); | |
$resp_headers = $resp[0]; | |
$resp_body = json_decode(end($resp)); | |
return [ | |
'request' => $info['request_header'] . $body, | |
'code' => $info['http_code'], | |
'headers' => $resp_headers, | |
'body' => $resp_body, | |
]; | |
} |
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
// Tymon\JWT snippets | |
// == generate token == // | |
$claims = [ | |
'exp' => JWTFactory::getTTL(), | |
'a' => $a, | |
'b' => $b | |
]; | |
$factory = JWTFactory::customClaims($claims); | |
$payload = $factory->make(); | |
$verifyToken = JWTAuth::encode($payload); | |
$tokenString = $verifyToken->get(); | |
// == verify token & check exp == // | |
try { | |
$payload = JWTAuth::getPayload($token); | |
if (Utils::timestamp($payload['exp'])->isPast()) { | |
// 401 response | |
return false; | |
} | |
} catch (JWTException $e) { | |
// 401 response; | |
return false; | |
} | |
// safely use $payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment