PHP Developers Network
http://forums.devnetwork.net/

PHP Bitbucket Profile
http://forums.devnetwork.net/viewtopic.php?f=50&t=142330
Page 1 of 1

Author:  630Studios [ Sun Feb 21, 2016 5:16 pm ]
Post subject:  PHP Bitbucket Profile

Hey everyone,

I just wrote a little PHP script to put on a websites to generate a profile from a supplied Bitbucket username.

It pulls out the accounts Avatar, First Name, Last Name and public repositories and puts them in a nice little display. Its hardly anything fancy but its a nice little drop in for a website that can save you a little bit of time.

Any feedback, suggestions for improvement, or "hey you totally botched this, you should of done it this way" is greatly appreciated.

The Function
Syntax: [ Download ] [ Hide ]
function generateBitBucketProfile($title, $accountName, $email)
{
        $jSonData = json_decode(file_get_contents("https://api.bitbucket.org/1.0/users/" . $accountName));
        ?>
    <div class="bitbucketProfile">
        <div  class="title"><?=$title;?></div>
        <img class="bitbucketAvatar" src="https://bitbucket.org/account/<?=$accountName;?>/avatar/64/" />
        <div>Created By: <?=$jSonData->user->first_name;?> <?=$jSonData->user->last_name;?></div>
        <div>Email: <?=$email;?></div>
        <div>Bitbucket: <a href="http://bitbucket.org/<?=$accountName;?>/" target="_blank">http://bitbucket.org/<?=$accountName;?>/</a></div>
        <div class="title">Repositories</div>
        <?php
        foreach ($jSonData->repositories as $repo)
        {
                ?>
                <div class="repo">
                <a href="http://bitbucket.org/<?=$accountName;?>/<?=$repo->slug;?>/"  target="_blank"><?=$repo->name;?></a><br />
                        <?=$repo->description?>
        </div>
        <?php
        }
}
 


The Bitbucket repo -
The repository has the script embedded in an actually web page with the needed styles to make it look nice. Since i wanted the entire thing to be contained in a single file the css is included in the head of the page instead of a separate file.

Demo of the script in action:


Appreciate any feedback.

Author:  Christopher [ Sun Feb 21, 2016 6:47 pm ]
Post subject:  Re: PHP Bitbucket Profile

I would prefer that the function returned a string, so it could be echoed or put into a template. Also, there is not error checking. What if web service fails and $jsonData does not contain valid data?

Author:  630Studios [ Sun Feb 21, 2016 9:02 pm ]
Post subject:  Re: PHP Bitbucket Profile

Hey,

Thanks for the reply.

Good point on the error checking, i'll fix that.

As far as having it return a string with the given layout the obvious simple answer is to copy, paste, and escape all the quotes. That however though is rather ugly, hackish, and then means you have to edit the layout twice.

The better solution I think might be to to change the function slightly to either accept a layout string that it then replaces special key words in and returns that string, or provide a path to a file to use as a template in which is replaces special keywords and returns as a string. Perhaps both.

Author:  Christopher [ Sun Feb 21, 2016 9:10 pm ]
Post subject:  Re: PHP Bitbucket Profile


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/