How to create a GitHub Gist with API?

use*_*582 4 php api gist github

By looking at GitHub Gist API, I understood that it is possible to create the Gist create for anonymous users without any API keys/authentication. Is it so?

I could not find answers to following questions:

  1. Are there any restrictions (number of gists) to be created etc?
  2. Is there any example that I can post the code from a form text input field to create a gist? I could not find any.

Thanks for any information about this.

Ama*_*ali 7

是.

来自Github API V3文档:

对于使用基本身份验证或OAuth的请求,您每小时最多可以请求5,000个请求.对于未经身份验证的请求,速率限制允许您每小时最多发出60个请求.

要创建要点,您可以POST按如下方式发送请求:

POST /gists
Run Code Online (Sandbox Code Playgroud)

这是我做的一个例子:

<?php
if (isset($_POST['button'])) 
{    
    $code = $_POST['code'];

    # Creating the array
    $data = array(
        'description' => 'description for your gist',
        'public' => 1,
        'files' => array(
            'foo.php' => array('content' => 'sdsd'),
        ),
    );                               
    $data_string = json_encode($data);

    # Sending the data using cURL
    $url = 'https://api.github.com/gists';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    # Parsing the response
    $decoded = json_decode($response, TRUE);
    $gistlink = $decoded['html_url'];

    echo $gistlink;    
}
?>

<form action="" method="post">
Code: 
<textarea name="code" cols="25" rows="10"/> </textarea>
<input type="submit" name="button"/>
</form>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档.