加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何使用API??创建GitHub Gist?

发布时间:2020-12-13 17:45:39 所属栏目:PHP教程 来源:网络整理
导读:通过查看 GitHub Gist API,我了解到可以为匿名用户创建Gist创建而无需任何API密钥/身份验证.是这样吗? 我找不到以下问题的答案: 是否有任何限制(要点数量)等? 是否有任何示例我可以从表单文本输入字段发布代码来创建一个要点?我找不到任何东西. 感谢您提
通过查看 GitHub Gist API,我了解到可以为匿名用户创建Gist创建而无需任何API密钥/身份验证.是这样吗?

我找不到以下问题的答案:

>是否有任何限制(要点数量)等?
>是否有任何示例我可以从表单文本输入字段发布代码来创建一个要点?我找不到任何东西.

感谢您提供相关信息.

解决方法

是.

从Github API V3文档:

For requests using Basic Authentication or OAuth,you can make up to 5,000 requests per hour. For unauthenticated requests,the rate limit allows you to make up to 60 requests per hour.

要创建一个要点,您可以发送POST请求,如下所示:

POST /gists

这是我做的一个例子:

<?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>

有关更多信息,请参阅documentation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读