Aureole Posted December 5, 2007 Share Posted December 5, 2007 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... Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted December 5, 2007 Share Posted December 5, 2007 You want information on Regular Expressions ( http://www.regular-expressions.info/php.html ). There is an entire forum dedicated to the topic on this site: http://www.phpfreaks.com/forums/index.php/board,43.0.html Quote Link to comment Share on other sites More sharing options...
revraz Posted December 5, 2007 Share Posted December 5, 2007 Searching google, CURL was a reply http://www.dbforums.com/showthread.php?t=1089113 Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 I tried one of the examples on the php website and I get a blank page, do I need to install curl or am I doing something wrong? I set error reporting to E_ALL and it's still a blank page... Quote Link to comment Share on other sites More sharing options...
rab Posted December 5, 2007 Share Posted December 5, 2007 <?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... Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 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. =) Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 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... Quote Link to comment Share on other sites More sharing options...
rab Posted December 5, 2007 Share Posted December 5, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 Alright, one thing I'm wondering is where the [2] comes from in all the arrays? $match[2] and I've never seen before syntax with two sets of [] like $match[2][0] ??? Thanks anyway! Quote Link to comment Share on other sites More sharing options...
rab Posted December 5, 2007 Share Posted December 5, 2007 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")); ?> Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 Hmm ok, I think I understand. Thanks once more... I'm really happy that this is actually possible. Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 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']); ?> Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 5, 2007 Author Share Posted December 5, 2007 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? Quote Link to comment Share on other sites More sharing options...
rab Posted December 6, 2007 Share Posted December 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 6, 2007 Author Share Posted December 6, 2007 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. =) Quote Link to comment Share on other sites More sharing options...
rab Posted December 6, 2007 Share Posted December 6, 2007 Its a multi-dimensional array, so $earned[1] is just an array. $earned[1][0] should have your achievements. Please read the docs at php.net Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 6, 2007 Author Share Posted December 6, 2007 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. 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... Quote Link to comment Share on other sites More sharing options...
rarebit Posted December 6, 2007 Share Posted December 6, 2007 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! Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 6, 2007 Author Share Posted December 6, 2007 Well maybe you're right but out of all the things that PHP has, arrays and all the things you can do with them/use them for confuse me the most (so far) except regex... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.