Search code examples
phpcurlgithub-api

PHP & cURL - GitHub API - 403


I'm completely new to cURL and API's, but i'm attempting to build an application that uses GitHubs Code Search API.

in my XAMPP Shell, I can execute the below string with no problems

curl -u myuser https://api.github.com/search/code?q=XXX+language:XXX?access_token=XXX

Now, as i'm trying to use cURL in PHP i run into some problems, my code below:

$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);

When i var_dump($result); The page greets me with a null value I checked the HTTP_CODE with the below code:

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

Which then tells me int(403) which is 403 Forbidden which relates to something authentication related. What exactly am i doing wrong in this situation?


Solution

  • Some little debugging would’ve pointed it out.

    If you don't send a user agent then you will get a 403 error, outputting $output yields:

    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.

    A quick look at the docs your also sending access_token which is not required, as your using auth basic.

    Ive changed the url and the following works fine:

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

    Result:

    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