Skip to content

Commit

Permalink
Merge pull request #22 from gerbenjacobs/v2
Browse files Browse the repository at this point in the history
Merging V2 onto master
  • Loading branch information
gerbenjacobs committed Dec 10, 2015
2 parents f21ea9e + b0f11eb commit ceb93fb
Show file tree
Hide file tree
Showing 20 changed files with 5,916 additions and 1,919 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php
php:
- 5.3
- 5.4
- 5.5
before_install:
- composer self-update
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[![Build status] (https://api.travis-ci.org/gerbenjacobs/HabboAPI.svg)](https://travis-ci.org/gerbenjacobs/HabboAPI)
[![Latest Stable Version](https://poser.pugx.org/gerbenjacobs/habbo-api/v/stable.svg)](https://packagist.org/packages/gerbenjacobs/habbo-api)
[![Join the chat at https://gitter.im/gerbenjacobs/HabboAPI](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gerbenjacobs/HabboAPI)
# HabboAPI
This PHP wrapper library is used to collect data from the _undcoumented_ Habbo API.
The project requires PHP 5.3 and uses the Composer autoloader and PSR-4 standard.

See the `example.php` file on how you could use this library.

## How to use it
1. Add [the Composer package](https://packagist.org/packages/gerbenjacobs/habbo-api) to your package.json file: `"gerbenjacobs/habbo-api": "v1.*"`
1. Add [the Composer package](https://packagist.org/packages/gerbenjacobs/habbo-api) to your package.json file: `"gerbenjacobs/habbo-api": "v2.*"`
2. On the page you want to use it add `include 'vendor/autoload.php'`
3. Create a HabboParser and construct it with the IP that the server runs on (to prevent JS Cookie issues) and the base URL of the API
4. Create a HabboAPI instance and inject the HabboParser
Expand All @@ -23,7 +24,7 @@ See the `example.php` file on how you could use this library.
use HabboAPI\HabboParser;

// Create new Parser and API instance
$habboParser = new HabboParser('https://www.habbo.com/api/public/');
$habboParser = new HabboParser('com');
$habboApi = new HabboAPI($habboParser);

// Find the user 'koeientemmer' and get their ID
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"nesbot/carbon": "^1.21"
},
"autoload": {
"psr-4": {
Expand Down
116 changes: 67 additions & 49 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
// Shortcut for the FQN
use HabboAPI\Entities\Badge;
use HabboAPI\Entities\Habbo;
use HabboAPI\Entities\Photo;
use HabboAPI\Entities\Profile;
use HabboAPI\HabboAPI;
use HabboAPI\HabboParser;

// Create new Parser and API instance
$habboParser = new HabboParser('https://www.habbo.com/api/public/');
$habboParser = new HabboParser('com');
$habboApi = new HabboAPI($habboParser);

// Find the user 'koeientemmer' and get their ID
Expand All @@ -31,71 +33,80 @@

if ($myHabbo->hasProfile()) {
// Collect all the profile info
/** @var Profile $myProfile */
$myProfile = $habboApi->getProfile($myHabbo->getId());
} else {
// This Habbo has a closed home, only show their Habbo object
$myProfile = array('habbo' => $myHabbo);
$profile = new Profile();
$myProfile = $profile->setHabbo($myHabbo);
}

$myPhotos = $habboApi->getPhotos($myHabbo->getId());

// Export as HTML
$html = [
'habbo' => '',
'worn_badges' => '',
'friends' => '',
'groups' => '',
'rooms' => '',
'badges' => ''
'badges' => '',
'photos' => ''
];

// Print all the $profile data in a pretty format, except for 'habbo'
$lastSection = 'habbo';
foreach ($myProfile as $section => $data) {

// Print section name
if ($section != $lastSection) {
$lastSection = $section;
$html[$section] .= '<h2>' . ucfirst($section) . ' (' . count($data) . ')</h2>';
}
// Some markup for the Habbo part

// Some markup for the Habbo part
if ($section == 'habbo') {
/* @var Habbo $habbo */
$habbo = $data;
$html['habbo'] .= '<img src="http://www.habbo.com/habbo-imaging/avatarimage?figure=' . $habbo->getFigureString() . '&size=l&gesture=sml&head_direction=3"
/* @var Habbo $habbo */
$habbo = $myProfile->getHabbo();
$html['habbo'] .= '<img src="http://www.habbo.com/habbo-imaging/avatarimage?figure=' . $habbo->getFigureString() . '&size=l&gesture=sml&head_direction=3"
alt="' . $habbo->getHabboName() . '" title="' . $habbo->getHabboName() . '" style="float: left; margin-right: 10px;" />';
$html['habbo'] .= '<h3>' . $habbo->getHabboName() . '</h3>';
$html['habbo'] .= '<p>' . $habbo->getMotto() . '<br><em>' . date('d-M-Y', strtotime($habbo->getMemberSince())) . '</em></p>';
if ($habbo->getProfileVisible()) {
$html['habbo'] .= '<p><a href="https://www.habbo.com/profile/' . $habbo->getHabboName() . '">View home &raquo;</a></p>';
}
if ($badges = $habbo->getSelectedBadges()) {
foreach ($badges as $badge) {
/** @var Badge $badge */
$html['worn_badges'] .=
'
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="http://images.habbo.com/c_images/album1584/' . $badge->getCode() . '.gif" alt="' . $badge->getName() . '">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">' . $badge->getName() . '</h4>
<em>' . $badge->getDescription() . '</em>
</div>
</div>
';
}
}
} else {
// Show all the other sections as an unordered list
if (in_array($section, array("friends", "groups", "rooms", "badges"))) {
$html[$section] .= '<ul>';
foreach ($data as $object) {
$html[$section] .= '<li>' . $object . '</li>'; // uses the __toString() method
}
$html[$section] .= '</ul>';
}
$html['habbo'] .= '<h3>' . $habbo->getHabboName() . '</h3>';
$html['habbo'] .= '<p>' . $habbo->getMotto() . '<br><em>' . $habbo->getMemberSince()->toFormattedDateString() . '</em></p>';
if ($habbo->getProfileVisible()) {
$html['habbo'] .= '<p><a href="https://www.habbo.com/profile/' . $habbo->getHabboName() . '">View home &raquo;</a></p>';
}
if ($badges = $habbo->getSelectedBadges()) {
foreach ($badges as $badge) {
/** @var Badge $badge */
$html['worn_badges'] .= '
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="http://images.habbo.com/c_images/album1584/' . $badge->getCode() . '.gif" alt="' . $badge->getName() . '">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">' . $badge->getName() . '</h4>
<em>' . $badge->getDescription() . '</em>
</div>
</div>
';
}
}

// Show all the other sections as an unordered list
foreach (array("friends", "groups", "rooms", "badges") as $section) {
$html[$section] .= '<ul>';
$method_name = sprintf('get%s', ucfirst($section));
foreach (call_user_func(array($myProfile, $method_name)) as $object) {
$html[$section] .= '<li>' . $object . '</li>'; // uses the __toString() method
}
$html[$section] .= '</ul>';
}

// Generate the photos
if ($myPhotos) {
/** @var Photo $myPhoto */
foreach ($myPhotos as $myPhoto) {
$html['photos'] .= '
<div class="col-md-3">
<a href="https://www.habbo.com/profile/' . $myPhoto->getCreatorName() . '/photo/' . $myPhoto->getId() . '" class="thumbnail">
<img src="' . $myPhoto->getPreviewUrl() . '" alt="' . $myPhoto->getId() . '">
</a>
<div class="caption">Taken on ' . $myPhoto->getTakenOn()->toFormattedDateString() . ' by ' . $myPhoto->getCreatorName() . '</div>
</div>
';
}
}
?>
Expand Down Expand Up @@ -147,7 +158,14 @@
</div>
</div>

<hr>

<div class="row">
<?php echo $html['photos']; ?>
</div>

<?php if ($myHabbo->hasProfile()): ?>
<hr>
<div class="row">
<div class="col-md-3">
<?php echo $html['badges']; ?>
Expand Down
9 changes: 6 additions & 3 deletions src/Entities/Habbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* The entitymodel for a Habbo object
*/
namespace HabboAPI\Entities;
use Carbon\Carbon;

/**
* Class Habbo
Expand All @@ -27,11 +28,13 @@ class Habbo implements Entity
*/
public function parse($data)
{
// These attributes are shared between Habbo and Friends
$this->setId($data['uniqueId']);
$this->setHabboName($data['name']);
$this->setMotto($data['motto']);
$this->setFigureString($data['figureString']);

// These could be missing..
if (isset($data['memberSince'])) {
$this->setMemberSince($data['memberSince']);
}
Expand Down Expand Up @@ -111,19 +114,19 @@ protected function setId($id)
}

/**
* @return mixed
* @return Carbon
*/
public function getMemberSince()
{
return $this->memberSince;
}

/**
* @param mixed $memberSince
* @param Carbon $memberSince
*/
protected function setMemberSince($memberSince)
{
$this->memberSince = $memberSince;
$this->memberSince = Carbon::parse($memberSince);
}

/**
Expand Down
Loading

0 comments on commit ceb93fb

Please sign in to comment.