PHP Bitbucket Profile

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
630Studios
Forum Newbie
Posts: 2
Joined: Sun Feb 21, 2016 12:55 pm

PHP Bitbucket Profile

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Bitbucket Profile

Post 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?
(#10850)
630Studios
Forum Newbie
Posts: 2
Joined: Sun Feb 21, 2016 12:55 pm

Re: PHP Bitbucket Profile

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Bitbucket Profile

Post 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.
(#10850)
Post Reply