Created
January 26, 2017 03:22
-
-
Save koike/a50583c5aa116481a8f4444d98cebda1 to your computer and use it in GitHub Desktop.
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 | |
require_once 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
class Gist | |
{ | |
public static function create(string $description, bool $public, array $files) | |
{ | |
$gist = | |
[ | |
'files' => $files, | |
'description' => $description, | |
'public' => $public | |
]; | |
$res = Gist::post('https://api.github.com/gists', $gist); | |
$body = $res->getBody()->getContents(); | |
$body = json_decode($body, true); | |
$url = $body['url'] ?? null; | |
if($url != null) | |
{ | |
$exp = explode('/', $url); | |
$end = end($exp); | |
$url = 'https://gist.github.com/anonymous/' . $end; | |
} | |
return $url; | |
} | |
private static function post($url, $data) | |
{ | |
$client = new Client(); | |
$res = $client | |
->post | |
( | |
$url, | |
[ | |
'json' => $data | |
] | |
); | |
return $res; | |
} | |
} | |
$files = | |
[ | |
"test.txt" => | |
[ | |
'content' => 'test body' | |
] | |
]; | |
$url = Gist::create('', false, $files); | |
echo $url . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment