Skip to content

Instantly share code, notes, and snippets.

@koike
Created January 26, 2017 03:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save koike/a50583c5aa116481a8f4444d98cebda1 to your computer and use it in GitHub Desktop.
<?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