David Stockdale's Scrapcode

GitHub Search Using PHP

In a recent technical test for a job application I had to create a GitHub search using PHP.

Unfortunately I didn’t get the job but I learned how to use Laravel and relearned how to use GitHub.

So at least I got something out of it.

Here’s a simple bit of PHP code which uses the GitHub API and curl to find a user by name.

It also returns the five repositories most recently starred by the user.

As well as the user’s top 5 repositories (In order of most to least starred).

				<?php
			 
                    $user = 'ussopking1';
                ?>



                <h1>
                    Test 1 (user)
                </h1>
<?php
                    $curl = curl_init();
                    curl_setopt_array($curl,[
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_URL => 'https://api.github.com/search/users?q=' . $user,
                        CURLOPT_USERAGENT => 'Github API in CURL'
                    ]);
                    $response1 = curl_exec($curl);
                    $response_array1 = ((array) json_decode($response1, true));
                    //print_r($response_array);
                    $key = 'items';
                    $value1 = $response_array1[$key][0];
                    // print_r($value);
                    $key2 ='login';
                    $name = $value1[$key2];
                    //print_r($name);
                    $key3 ='url';
                    $url = $value1[$key3];
                    $key4 ='avatar_url';
                    $img_url = $value1[$key4];
                    $key5 ='score';
                    $score = $value1[$key5];
                    
                    ?>

                        <img src= <?php echo $img_url ?> 
                        alt="" style="width: 150px;" class="feature-logo"/>

                        <p>
                        <?php 
                            print_r("Score: " .$score);
                            echo  nl2br ("\n");
                        ?>
                        </p>


                    <a href="
                    <?php
                        print_r($url);
                    ?>
                    " class="test">
                    <?php
                        print_r($name);
                    ?>

                </a>
                    <?php
                    curl_close($curl);
                ?>
                
                <h2>
                    Repositories Recently Starred By User (In order of most recent to oldest):
                </h2> 

                <p style="overflow-wrap: break-word;">
                    <?php
                    try {
                        $curl1 = curl_init();
    
                        curl_setopt_array($curl1,[
                            CURLOPT_RETURNTRANSFER => 1,
                            CURLOPT_URL => 'https://api.github.com/users/' . $user . '/starred?page=1&per_page=5',
                            CURLOPT_USERAGENT => 'Github API in CURL'
                        ]);
                        $response2 = curl_exec($curl1);
                        $response_array2 = ((array) json_decode($response2, true));
                        $num_of_items1 = count($response_array2);
                        for ($i=0; $i < $num_of_items1; $i++) { 
                            $response_item1 = $response_array2[$i];
                            $key = 'name';
                            $value2 = $response_item1[$key];
                            echo $value2;
                            if($i < ($num_of_items1-1)) {
                                echo  nl2br ("\n");
                            }
                        }
                        curl_close($curl1);
                    } catch (Exception $e) {
                        echo 'Caught exception: ',  $e->getMessage(), "\n";
                    }
                    ?>
                    </p>

                    <h2>
                        User's Top 5 Repositories (In order of most to least starred):
                    </h2>

                    <p style="overflow-wrap: break-word;">
                        <?php
                    try {
                        $curl2 = curl_init();
        
                            curl_setopt_array($curl2,[
                                CURLOPT_RETURNTRANSFER => 1,
                                CURLOPT_URL => 'https://api.github.com/users/' . $user . '/repos',
                                CURLOPT_USERAGENT => 'Github API in CURL'
                            ]);
                            $response = curl_exec($curl2);
    
                            $response_array3 = ((array) json_decode($response, true));
                            $sorted = array_column($response_array3, 'stargazers_count');
                            array_multisort($sorted, SORT_DESC, $response_array3, SORT_DESC);
                            $num_of_items = count($response_array3);
                            $size_limit = 5;
                            for ($i=0; $i < $num_of_items && $i < $size_limit; $i++) { 
                                $response_item = $response_array3[$i];
                                $key = 'name';
                                $value3 = $response_item[$key];
                                echo $value3;
                                $key2 = 'stargazers_count';
                                $value4 = $response_item[$key2];
                                echo " (Stars: ".$value4.")";
                                if($i < ($num_of_items-1)) {
                                    echo  nl2br ("\n");
                                }
                            }
                            curl_close($curl2);
                    } catch (Exception $e) {
                        echo 'Caught exception: ',  $e->getMessage(), "\n";
                    }
						?>
					</p>

Leave a Reply