PHP & cURL - GitHub API - 403

PHP & cURL - GitHub API - 403

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

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

curl -u myuser https://api.github.com/search/code?q=XXX+language:XXX?access_token=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);

当我 var_dump($result); 页面以空值迎接我 我用下面的代码检查了 HTTP_CODE:

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

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

稍微调试一下就会指出来。

如果您不发送用户代理,那么您将收到 403 错误,输出 $output 会产生:

Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.

快速查看您还发送的文档 access_token,这不是必需的,因为您使用的是基本身份验证。

我更改了 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>';

结果:

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