PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 20, 2017 11:41 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: PHP Bitbucket Profile
PostPosted: Sun Feb 21, 2016 5:16 pm 
Offline
Forum Newbie

Joined: Sun Feb 21, 2016 1:55 pm
Posts: 2
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 - 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.voxelstudio3d.com/bitbucketProfile.php


Appreciate any feedback.


Top
 Profile  
 
PostPosted: Sun Feb 21, 2016 6:47 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13443
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Sun Feb 21, 2016 9:02 pm 
Offline
Forum Newbie

Joined: Sun Feb 21, 2016 1:55 pm
Posts: 2
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.


Top
 Profile  
 
PostPosted: Sun Feb 21, 2016 9:10 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13443
Location: New York, NY, US
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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group