PHP&;cURL-githubapi-403

PHP&;cURL-githubapi-403,php,curl,github-api,Php,Curl,Github Api,我对cURL和API是完全陌生的,但我正在尝试构建一个使用cURL和API的应用程序 在我的XAMPP Shell中,我可以毫无问题地执行下面的字符串 curl-u myuserhttps://api.github.com/search/code?q=XXX+语言:XXX?访问令牌=XXX 现在,当我尝试在PHP中使用cURL时,我遇到了一些问题,我的代码如下: $url = 'https://api.github.com/search/code?q=' . $term . 'language:

我对cURL和API是完全陌生的,但我正在尝试构建一个使用cURL和API的应用程序

在我的XAMPP Shell中,我可以毫无问题地执行下面的字符串

curl-u myuserhttps://api.github.com/search/code?q=XXX+语言:XXX?访问令牌=XXX

现在,当我尝试在PHP中使用cURL时,我遇到了一些问题,我的代码如下:

$url = 'https://api.github.com/search/code?q=' . $term . 'language:' . $lang . 'stars:' . $stars . '?access_token=' . $token;
$cInit = curl_init();
curl_setopt($cInit, CURLOPT_URL, $url);
curl_setopt($cInit, CURLOPT_RETURNTRANSFER, 1); // 1 = TRUE
curl_setopt($cInit, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cInit, CURLOPT_USERPWD, $user . ':' . $pwd);

$output = curl_exec($cInit);
$info = curl_getinfo($cInit, CURLINFO_HTTP_CODE);
$result = json_decode($output);

curl_close($cInit);
当i
var\u dump($result)时页面用空值向我打招呼
我用下面的代码检查了HTTP_代码:

$info = curl_getinfo($cInit, CURLINFO_HTTP_CODE);
var_dump($info);

然后告诉我
int(403)
这是
403禁止的
,它与身份验证相关。在这种情况下,我到底做错了什么?

一些小的调试会指出这一点

如果不发送用户代理,则会出现403错误,输出
$output
将产生:

行政法规禁止的请求。请确保您的 请求具有用户代理标头 (). 检查 因为其他可能的原因

快速查看您使用auth basic时发送的
访问\u令牌
的文档,这不是必需的

我已经更改了url,以下各项工作正常:

<?php
$user = 'Your Github Username';
$pwd = 'Your Github Password';

$url = 'https://api.github.com/search/code?q=addClass+in:file+language:js+repo:jquery/jquery';
$cInit = curl_init();
curl_setopt($cInit, CURLOPT_URL, $url);
curl_setopt($cInit, CURLOPT_RETURNTRANSFER, 1); // 1 = TRUE
curl_setopt($cInit, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($cInit, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cInit, CURLOPT_USERPWD, $user . ':' . $pwd);

$output = curl_exec($cInit);

$info = curl_getinfo($cInit, CURLINFO_HTTP_CODE);
$result = json_decode($output);

curl_close($cInit);

echo '<pre>'.print_r($result, true).'</pre>';

我所缺少的只是用户代理?哇!我想我还有更多的阅读要做。在命令行上使用cURL将使用默认的有效用户代理,根据您的主机设置,可能会/可能不会设置。容易错过。另外,您试图对非json值进行
json\u解码,因此产生了
null
结果,以及您错过错误消息的原因。不,这一切都很好,它没有在文档上明确说明,但在其他地方,很容易错过。当一个响应没有json_解码时,将其转储到屏幕上,尽管您可能认为响应是json格式的,而不是纯文本lol格式的
stdClass Object
(
    [total_count] => 8
    [incomplete_results] => 
    [items] => Array
        (
            [0] => stdClass Object
                (
                    [name] => classes.js
                    [path] => src/attributes/classes.js
                    [sha] => 0c90a8dffdb95ec4678f18e07623f9d111c32540
                    [url] => https://api.github.com/repositories/167174/contents/src/attributes/classes.js?ref=3d732cca6b5076a9d13eee98e2b075b37384cd91
                    [git_url] => https://api.github.com/repositories/167174/git/blobs/0c90a8dffdb95ec4678f18e07623f9d111c32540
                    [html_url] => https://github.com/jquery/jquery/blob/3d732cca6b5076a9d13eee98e2b075b37384cd91/src/attributes/classes.js
                    [repository] => stdClass Object
                        (
                            [id] => 167174
                            [name] => jquery
                            [full_name] => jquery/jquery
                            [owner] => stdClass Object
                                (
                                    [login] => jquery
                                    [id] => 70142
                                    [avatar_url] => https://avatars1.githubusercontent.com/u/70142?v=4
                                    [gravatar_id] => 
                                    [url] => https://api.github.com/users/jquery
                                    [html_url] => https://github.com/jquery
                                    [followers_url] => https://api.github.com/users/jquery/followers
                                    [following_url] => https://api.github.com/users/jquery/following{/other_user}
                                    [gists_url] => https://api.github.com/users/jquery/gists{/gist_id}
                                    [starred_url] => https://api.github.com/users/jquery/starred{/owner}{/repo}
                                    [subscriptions_url] => https://api.github.com/users/jquery/subscriptions
                                    [organizations_url] => https://api.github.com/users/jquery/orgs
                                    [repos_url] => https://api.github.com/users/jquery/repos
                                    [events_url] => https://api.github.com/users/jquery/events{/privacy}
                                    [received_events_url] => https://api.github.com/users/jquery/received_events
                                    [type] => Organization
                                    [site_admin] => 
                                )

                            [private] => 
                            [html_url] => https://github.com/jquery/jquery
                            [description] => jQuery JavaScript Library
                            [fork] => 
                            [url] => https://api.github.com/repos/jquery/jquery
                            [forks_url] => https://api.github.com/repos/jquery/jquery/forks
                            [keys_url] => https://api.github.com/repos/jquery/jquery/keys{/key_id}
                            [collaborators_url] => https://api.github.com/repos/jquery/jquery/collaborators{/collaborator}
                            [teams_url] => https://api.github.com/repos/jquery/jquery/teams
                            [hooks_url] => https://api.github.com/repos/jquery/jquery/hooks
                            [issue_events_url] => https://api.github.com/repos/jquery/jquery/issues/events{/number}
                            [events_url] => https://api.github.com/repos/jquery/jquery/events
                            [assignees_url] => https://api.github.com/repos/jquery/jquery/assignees{/user}
                            [branches_url] => https://api.github.com/repos/jquery/jquery/branches{/branch}
                            [tags_url] => https://api.github.com/repos/jquery/jquery/tags
                            [blobs_url] => https://api.github.com/repos/jquery/jquery/git/blobs{/sha}
                            [git_tags_url] => https://api.github.com/repos/jquery/jquery/git/tags{/sha}
                            [git_refs_url] => https://api.github.com/repos/jquery/jquery/git/refs{/sha}
                            [trees_url] => https://api.github.com/repos/jquery/jquery/git/trees{/sha}
                            [statuses_url] => https://api.github.com/repos/jquery/jquery/statuses/{sha}
                            [languages_url] => https://api.github.com/repos/jquery/jquery/languages
                            [stargazers_url] => https://api.github.com/repos/jquery/jquery/stargazers
                            [contributors_url] => https://api.github.com/repos/jquery/jquery/contributors
                            [subscribers_url] => https://api.github.com/repos/jquery/jquery/subscribers
                            [subscription_url] => https://api.github.com/repos/jquery/jquery/subscription
                            [commits_url] => https://api.github.com/repos/jquery/jquery/commits{/sha}
                            [git_commits_url] => https://api.github.com/repos/jquery/jquery/git/commits{/sha}
                            [comments_url] => https://api.github.com/repos/jquery/jquery/comments{/number}
                            [issue_comment_url] => https://api.github.com/repos/jquery/jquery/issues/comments{/number}
                            [contents_url] => https://api.github.com/repos/jquery/jquery/contents/{+path}
                            [compare_url] => https://api.github.com/repos/jquery/jquery/compare/{base}...{head}
                            [merges_url] => https://api.github.com/repos/jquery/jquery/merges
                            [archive_url] => https://api.github.com/repos/jquery/jquery/{archive_format}{/ref}
                            [downloads_url] => https://api.github.com/repos/jquery/jquery/downloads
                            [issues_url] => https://api.github.com/repos/jquery/jquery/issues{/number}
                            [pulls_url] => https://api.github.com/repos/jquery/jquery/pulls{/number}
                            [milestones_url] => https://api.github.com/repos/jquery/jquery/milestones{/number}
                            [notifications_url] => https://api.github.com/repos/jquery/jquery/notifications{?since,all,participating}
                            [labels_url] => https://api.github.com/repos/jquery/jquery/labels{/name}
                            [releases_url] => https://api.github.com/repos/jquery/jquery/releases{/id}
                            [deployments_url] => https://api.github.com/repos/jquery/jquery/deployments
                        )

                    [score] => 0.5187095
                )
//.. snip