简体   繁体   English

在github API上使用curl出错

[英]Got error using curl with github api

I am using the curl to get git repositories information. 我正在使用curl获取git存储库信息。 The command used is: 使用的命令是:

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

However, what I got is: 但是,我得到的是:

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

If I type directly the URL in the browser I obtain the correct JSON output, but when I use curl it fails. 如果我直接在浏览器中键入URL,则可以获得正确的JSON输出,但是当我使用curl时,它将失败。 Any solution? 有什么办法吗?

In curl's documentation they actually only show in one of their examples: curl的文档中,它们实际上仅在其示例之一中显示:

Get the main page from an IPv6 web server: curl "http://[2001:1890:1112:1::20]/" 从IPv6 Web服务器获取主页: curl "http://[2001:1890:1112:1::20]/"

"" are required to surround your URL (due to its complexity). 网址必须包含"" (由于其复杂性)。 The command should be like this then: 该命令应如下所示:

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

If you want it to be downloaded to a file, then you can use the -O option: 如果希望将其下载到文件中,则可以使用-O选项:

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

If you're just using windows command line then you can run the winssl 's curl from this zip (you need the ssl version for https ). 如果您仅使用Windows命令行,则可以从此zip运行winssl的curl(您需要https的ssl版本)。

However, I noticed your javascript tag in your question, so if you're planning to use curl on the client side, the curl.js plugin may be useful to you. 但是,我在问题中注意到了您的javascript标记,因此,如果您打算在客户端使用curl, curl.js插件可能对您有用。

On the server side, if you are using PHP you can use this code: 在服务器端,如果您使用的是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)); ?> 

This code was placed by "none" in here , but in your case, you may need SSL verification enabled (so I commented it). 这段代码在这里“ none”放置的,但是在您的情况下,您可能需要启用SSL验证(因此我评论了它)。

curl_setopt allows you to set these required options (and the URL itself) when using curl for getting a json file. curl_setopt允许您在使用curl获取json文件时设置这些必需的选项(以及URL本身)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM