Page 1 of 1

PHP Bitbucket Profile

Posted: Sun Feb 21, 2016 4:16 pm
by 630Studios
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

Code: Select all

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 - https://bitbucket.org/630Studios/php-bitbucketprofile
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: http://phpaccountframework.voxelstudio3 ... rofile.php


Appreciate any feedback.

Re: PHP Bitbucket Profile

Posted: Sun Feb 21, 2016 5:47 pm
by Christopher
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?

Re: PHP Bitbucket Profile

Posted: Sun Feb 21, 2016 8:02 pm
by 630Studios
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.

Re: PHP Bitbucket Profile

Posted: Sun Feb 21, 2016 8:10 pm
by Christopher
630Studios wrote: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.
Maybe you could try a heredoc is your sensibilities are offended by double quoted strings.
630Studios wrote: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.
If the layout is independent then the function should just do error checking and return an object. That would make it a clean Model object. You could then pass that object to any template.