Jump to content

I pose a question for you...


Aureole

Recommended Posts

Would it be possible using PHP to create some kind of reader that would:

 

Go to a Website like this.

 

Find something in the source, like this:

 

<li>Highest Skill: <span id="ctl00_mainContent_identityStrip_lblSkill">xx</span>

 

...and return whatever is within the span tag (where xx is)?

 

Of course this is just a very small example... if this is possible would it be difficult and if it is does anyone have some example code?

 

 

There has to be some way to do things like this... thanks.

 

EDIT:

 

Of course I would have it so a cron job ran every 24 Hours or so to grab the information for each user, so I wouldn't be using too much bandwidth for the site in question. It's just they don't provide RSS Feeds for the data I need...

Link to comment
Share on other sites

<?php

function fetchHaloStats($gamertag) {
    if(empty($gamertag)) 
        return False;

    $stats = file_get_contents("http://www.bungie.net/Stats/Halo3/Default.aspx?player=".urlencode($gamertag));

    if(preg_match('/Halo 3 Service Record Not Found/i', $stats)) 
        return False;

    preg_match_all('/<span id="ctl00_mainContent_identityStrip_lbl(ServiceTag|Skill|Rank|TotalRP)">(.*?)<\/span>/', $stats, $match);
    return array('Tag'  => $match[2][0], 
                 'Rank' => $match[2][1],
                 'Skill'=> $match[2][2],
                 'EXP'  => $match[2][3]);

}

var_dump(fetchHaloStats("xrab"));
?>

In action...

rab@death:~$ php halo3.php
array(4) {
  ["Tag"]=>
  string(3) "R23"
  ["Rank"]=>
  string(16) "Captain, Grade 1"
  ["Skill"]=>
  string(2) "23"
  ["EXP"]=>
  string(3) "129"
}

 

There ya go, there is always room for expansion with this...

 

Link to comment
Share on other sites

Never mind, I wasn't outputting anything to the browser hence why nothing was showing. But this is still very confusing to me.

 

Does anyone have an example of how to do this? It's all good posting links (which I did read) but they don't really help as much as seeing the actual code in action... that's how I learn.

 

Thanks for the help so far anyway.

 

EDIT: Thanks a lot rab, I'll give this a test now. =)

Link to comment
Share on other sites

That works beautifully, now I'm trying to modify your example to just return one thing instead of the array and I'm not having any luck.

 

<?php
function fetchHaloStats($gamertag)
{
   if(empty($gamertag))
   {
       return false;
   }

   $stats = file_get_contents('http://www.bungie.net/Stats/Halo3/Default.aspx?player='.urlencode($gamertag));

   if(preg_match('/Halo 3 Service Record Not Found/i', $stats))
   {
       return false;
}

preg_match('/<span id="ct100_mainContent_identityStrip_lblServiceTag">(.*?)<\/span>/', $stats, $match);
echo('Your Service Tag is: '.$match);
}

fetchHaloStats($_GET['gamertag']);
?>

 

Returns: "Your Service Tag is: Array"

 

Thanks so much for your help so far...

 

EDIT: I don't understand this it seems... but I'm working on it...

Link to comment
Share on other sites

<?php

function fetchHaloStats($gamertag, $specific='') {
    if(empty($gamertag)) 
        return False;

    $stats = file_get_contents("http://www.bungie.net/Stats/Halo3/Default.aspx?player=".urlencode($gamertag));

    if(preg_match('/Halo 3 Service Record Not Found/i', $stats)) 
        return False;

    preg_match_all('/<span id="ctl00_mainContent_identityStrip_lbl(ServiceTag|Skill|Rank|TotalRP)">(.*?)<\/span>/', $stats, $match);
    $stats =  array('Tag'  => $match[2][0], 
                    'Rank' => $match[2][1],
                    'Skill'=> $match[2][2],
                    'EXP'  => $match[2][3]);
    return (!empty($specific) && array_key_exists($specific,$stats) ? $stats[$specific] : $stats);

}

var_dump(fetchHaloStats("xrab","Tag"));
?>

 

Slightly modified.

 

The reason why your getting "Array" back is because preg_match fills the third argument, $match, with the matched text.

If matches  is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1]  will have the text that matched the first captured parenthesized subpattern, and so on.

 

So $matches[1][0] should have the service tag.

Link to comment
Share on other sites

I'm matching 2 things, (ServiceTag|Skill|Rank|TotalRP) and (.*?), which fills $match. The first match isn't needed, so i _should_ make it (?:...) so it doesn't back reference that text.

 

<?php

function fetchHaloStats($gamertag, $specific='') {
    if(empty($gamertag)) 
        return False;

    $stats = file_get_contents("http://www.bungie.net/Stats/Halo3/Default.aspx?player=".urlencode($gamertag));

    if(preg_match('/Halo 3 Service Record Not Found/i', $stats)) 
        return False;

    preg_match_all('/<span id="ctl00_mainContent_identityStrip_lbl(?:ServiceTag|Skill|Rank|TotalRP)">(.*?)<\/span>/', $stats, $match);
    $stats =  array('Tag'  => $match[1][0], 
                    'Rank' => $match[1][1],
                    'Skill'=> $match[1][2],
                    'EXP'  => $match[1][3]);
    return (!empty($specific) && array_key_exists($specific,$stats) ? $stats[$specific] : $stats);

}

var_dump(fetchHaloStats("xrab","Tag"));
?>

Link to comment
Share on other sites

Ok I'm playing around with it to try understand how it all works... now I have all of the following working except the player model, I'm obviously doing something wrong but I don't know what.

 

<?php

function fetchHaloStats($gamertag, $specific='') 
{
    if(empty($gamertag))
    {
	echo('<p>You didn\'t enter a Gamertag.</p>');
	exit;
}

    $stats = file_get_contents('http://www.bungie.net/Stats/Halo3/Default.aspx?player='.urlencode($gamertag));

    if(preg_match('/Halo 3 Service Record Not Found/i', $stats))
    {
	echo('<p>The Gamertag you entered does not have a Service Record associated with it on Bungie.net.</p>');
	exit;
}

$playermodel = preg_match('/<img id="ctl00_mainContent_imgModel" src="(.*?)" style="border-width:0px;" \/>/', $stats);

    preg_match_all('/<span id="ctl00_mainContent_identityStrip_lbl(ServiceTag|Skill|Rank|TotalRP)">(.*?)<\/span>/', $stats, $match);

    $stats =  array('Service Tag' => $match[2][0], 'EXP Rank' => $match[2][1], 'Highest Skill'=> $match[2][2], 'Total EXP'  => $match[2][3]);

echo('<p><strong>Gamertag</strong>: '. $_GET['gamertag']."</p>\n");

echo('<img src="http://www.bungie.net/'.$playermodel.'" style="border:0 none;" />');

    foreach($stats as $prop => $val)
{
	echo('<p><strong>'.$prop.'</strong>: '.$val."</p>\n");
}
}

fetchHaloStats($_GET['gamertag']);
?>

Link to comment
Share on other sites

Sorry for the triple post but when I edit my posts it messes with everything contained within code tags.

 

I figured it out by changing:

 

$playermodel = preg_match('/<img id="ctl00_mainContent_imgModel" src="(.*?)" style="border-width:0px;" \/>/', $stats);

 

...to:

 

$playermodel = preg_match('/<img id="ctl00_mainContent_imgModel" src="(.*?)" style="border-width:0px;" \/>/', $stats, $pmodel);

 

...then changing:

 

echo('<img src="http://www.bungie.net/'.$playermodel.'" style="border:0 none;" />');

 

...to:

 

echo('<img src="http://www.bungie.net/'.$pmodel[1].'" style="border:0 none;" />');

 

Is that the right way to do it?

Link to comment
Share on other sites

If it works, then sure. It might not be the best way, but the way you altered the function isn't the best way either. You should modify the function to return what you want then go on with the script there.

 

<?php

$stats = fetchHaloStats($_GET['gamertag']);

if( $stats === False ) {
  if( empty($_GET['gamertag']) ) {
    // need a tag
   } else {
    // Invalid tag
   }
}

// print out the stats now. 
?>

 

In my opinion, it's not good to set a function to do error handling, depends on the error handling, and exiting the program. That should all be done outside the funciton.

Link to comment
Share on other sites

Well I don't really understand how it all works but I just wanted to play around with preg_match and what-not. =P

 

One thing that's confusing me... I got the preg_match to work for the player model yet it refuses to work for something else I tried.

 

Here's the basics of it (I took it out of the function to make it simpler...

 

<?php
$gamertag = 'TxV Aureole';

$stats = file_get_contents('http://www.bungie.net/Stats/Halo3/Default.aspx?player='.urlencode($gamertag));

$earnedachiev = preg_match('/<td class="col2">(.*?) of 49<\/td>/', $stats, $earned);

echo('<p>Earned '.$earned[1].' of 49 achievements');
?>

 

That shows "Earned of 49 achievements".

 

I'll take your advise on the error checking and what-not but first I need to understand the preg_match() stuff a bit more and work out why it works sometimes and not others. =)

Link to comment
Share on other sites

Alright I'll go over there and look into arrays more and about multi-dimensional arrays... I find arrays hard to understand but hey...

 

Oh and $earned[1][0] didn't work just so you know. :D

 

Thanks a lot.

 

I realize I'm probably getting annoying now but may I ask why...

 

<?php
$stats = file_get_contents('http://www.bungie.net/Stats/Halo3/Default.aspx?player='.urlencode($gamertag));

// This works...

preg_match('/<img id="ctl00_mainContent_imgModel" src="(.*?)" style="border-width:0px;" \/>/', $stats, $pmodel);
echo $pmodel[0];

// ...yet this doesn't...

preg_match('/Game History ((.*?))/', $stats, $gamehistory);
echo $gamehistory[0];

// ... nor the earned thing either $earned[1] or $earned[1][0]
?>

 

If the earned achievements was a multi dimensional array then wouldn't the player model also be one, so how come that works just as $pmodel[0]? ???

 

Sorry for all the questions...

Link to comment
Share on other sites

Before I started programming, I was a designer using autocad and 3ds, in those you have array's as well (including polar array's (great for making spiral staircase's)). For instance I could say the tiles on my bathroom wall are a 2 dimensional array, a high-rise block of apartments could be a 3 dimensional array. The problem with array's is that they sound scary, just think of them as list's, or a notebook full of list's...

 

Sorry if this sound patronising, it's not meant to be... however American spell checkers are!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.