javascript - 使用curl和github api时出现错误

标签 javascript git curl

我正在使用curl 来获取git 存储库信息。 使用的命令是:

curl https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc

但是,我得到的是:

{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "Search",
      "field": "q",
      "code": "missing"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/search"
}

如果我直接在浏览器中输入 URL,我会获得正确的 JSON 输出,但是当我使用curl 时,它会失败。有什么解决办法吗?

最佳答案

curl's documentation他们实际上只在其中一个示例中显示:

从 IPv6 Web 服务器获取主页:curl "http://[2001:1890:1112:1::20]/"

<强> ""需要将您的网址括起来(由于其复杂性)。命令应该是这样的:

curl "https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc"

如果您希望将其下载到文件中,则可以使用 -O选项:

curl -O "https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc"

如果你只是using windows命令行然后你可以运行 winssl的 curl 来自 this zip (您需要 https 的 ssl 版本)。

但是,我注意到你的javascript标签在你的问题中,所以如果你打算在客户端使用curl,curl.js plugin可能对你有用。

在服务器端,如果您使用PHP,则可以使用以下代码:

<?
    $url="https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc";
    
    //  Initiate curl
    $ch = curl_init();
    // Disable SSL verification
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set the url
    curl_setopt($ch, CURLOPT_URL,$url);
    // Execute
    $result=curl_exec($ch);
    // Closing
    curl_close($ch);
    
    // Will dump a beauty json :3
    var_dump(json_decode($result, true));
?>

此代码由 "none" in here 放置,但就您而言,您可能需要启用 SSL 验证(所以我评论了它)。

curl_setopt允许您在使用curl 获取json 文件时设置这些必需的选项(以及URL 本身)。

关于javascript - 使用curl和github api时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555412/

相关文章:

JavaScript 调用 - 我可以直接访问对象属性

javascript - JS 脚本无法通过 Hooks 加载到 WordPress 中

git - git 修订树的图形动态 View

git - 在 Mac 上更改 Git 存储库下载路径

linux - 创建一个 shell,使用 curl 在 dropfile.to 上下载文件

javascript - 在脚本标签内传递 response.render 的值

验证数组内容的 Javascript 函数

git - "git pull --all"可以更新我所有的本地分支吗?

curl - 如何解决 404 错误 : Not Found in swagger while testing the api in Laravel 6

无法识别 CURL apache 模块中的自定义 header