hub_get_contents()
Due to the lack of activity this guild has been experiencing. I've decided to release three quarters of a PHP function I wrote a while ago.
A lot of people tend to ask how to grab hub page information. The best way is PHP and the BYOND Hub Page text format. With this in mind, I wrote a function some time ago which did it. But it was a crap function.
Since then, I've rewritten it twice. And even released one to a select few of BYOND Users. Now I'm going to release part of that function (after it was revised) to everyone.
And here it is: [Due to a bug inside the DM parser in BYOND Members pages, you'll have to remove the anchor tags inside the file_get_contents() function.]
The full function includes server information (who's logged into what server and all that). But I'm not in the mood to give away all my secrets.
(This could probably be done better using Regular Expressions, but because I don't know them, I'm not going to use them.)
To use the function, just call your hub variable like an array: echo $hub['title'];
Easy done.
--
If you're already PHP compliant, I'm also including a copy of the function without the comments.
A lot of people tend to ask how to grab hub page information. The best way is PHP and the BYOND Hub Page text format. With this in mind, I wrote a function some time ago which did it. But it was a crap function.
Since then, I've rewritten it twice. And even released one to a select few of BYOND Users. Now I'm going to release part of that function (after it was revised) to everyone.
And here it is: [Due to a bug inside the DM parser in BYOND Members pages, you'll have to remove the anchor tags inside the file_get_contents() function.]
<?
//Using the standard world/hub format. Just type in the key and the game name as shown below.
$hub = @hub_get_contents('tiberath.storytelling');
function hub_get_contents($hub_page) {
//We use the file_get_contents() function to grab the text information off the BYOND Hub page.
$hub_page = @file_get_contents('http://games.byond.com/hub/hub.cgi?qd=hub;hub=' . $hub_page . ';format=text');
//We use an array to store the information we wish to return back to the $hub variable. You can remove anything you think you wont need.
//But if you look, you'll notice the words match exactly what is displayed on the text format of a hub page. It has to be that way.
//Bare in mind, I haven't included the 'subscription_price' option yet. That'll probably come in a later release.
$game_info = array("title" => "", "icon" => "", "small_icon" => "", "short_desc" => "", "long_desc" => "", "author" => "", "registered" => "", "version" => "", "status" => "", "byond_rank" => "");
//I used to use a while() loop here. But I later learned this makes the code more compact and probably works better.
foreach($game_info as $key => $value) {
$start = strpos($hub_page, $key);
//Because BYOND's text format is a pain, it sometimes doesn't enclose it's values in quotation marks. When it doesn't,
//it breaks my function. So we have to distinguish when it does and doesn't. Luckily, it only doesn't once.
//We also have to make sure there is a start value. If the value we're looking for isn't present, there's no sense running
//a function to find it's contents.
if($start) {
if($key == 'byond_rank') {
$start += 3;
$end = strpos($hub_page, (chr(10)), $start);
} else {
$start += 4;
$end = strpos($hub_page, ('"' . chr(10)), $start);
}
//To remove the key from the start, we add it's length to the start variable.
$start += strlen($key);
//I used to use Mobius' substring() function here. But I decided there wasn't going to be a need when there wouldn't be an end variable.
//So I just did the logical thing. Take the start from the end. Beautiful no?
$game_info[$key] = stripslashes(str_replace("\\n", "<br>", substr($hub_page, $start, $end - $start)));
}
}
//Return the game_info array so you can use the hub variable as an array.
return $game_info;
}
?>
|
The full function includes server information (who's logged into what server and all that). But I'm not in the mood to give away all my secrets.
(This could probably be done better using Regular Expressions, but because I don't know them, I'm not going to use them.)
To use the function, just call your hub variable like an array: echo $hub['title'];
Easy done.
--
If you're already PHP compliant, I'm also including a copy of the function without the comments.
<?
$hub = @hub_get_contents('tiberath.storytelling');
function hub_get_contents($hub_page) {
$hub_page = @file_get_contents('http://games.byond.com/hub/hub.cgi?qd=hub;hub=' . $hub_page . ';format=text');
$game_info = array("title" => "", "icon" => "", "small_icon" => "", "short_desc" => "", "long_desc" => "", "author" => "", "registered" => "", "version" => "", "status" => "", "byond_rank" => "");
foreach($game_info as $key => $value) {
$start = strpos($hub_page, $key);
if($start) {
if($key == 'byond_rank') {
$start += 3;
$end = strpos($hub_page, (chr(10)), $start);
} else {
$start += 4;
$end = strpos($hub_page, ('"' . chr(10)), $start);
}
$start += strlen($key);
$game_info[$key] = stripslashes(str_replace("\\n", "<br>", substr($hub_page, $start, $end - $start)));
}
}
return $game_info;
}
?>
|
Posted by Tiberath (Guildmaster) on Monday, December 17, 2007 09:48PM
- 3 comments
(link)
/
Keywords:
tibscript
(Edited on Monday, December 17, 2007 09:53PM)
« PHP -> PHP Communication Function · Online Games (Updated!) »
Login to post a comment.
#3 Tiberath:
The extra tidbids really are a piece of cake. I'll probably release the full script some day.
Tuesday, December 18, 2007 04:29PM
#2 Vermolius:
Neat. I'll definitely be using that sometime in the future. Possibly trying to figure out those extra tricks you were talking about too.
Tuesday, December 18, 2007 08:11AM
#1 Tiberath:
Due to a bug inside the DM parser in BYOND Members pages, you'll have to remove the anchor tags inside the file_get_contents() function.
Monday, December 17, 2007 09:52PM