You are viewing our Forum Archives. To view or take place in current topics click here.
[PHP] Get Data from a remote div
Posted:

[PHP] Get Data from a remote divPosted:

Z61
  • TTG Fanatic
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Im trying to get the data from a div on 'Gamercard.xbox.com/{GAMERTAG}.card' but I cannot seem to get it to get the data correctly, can anyone help me?
im trying to get the text inside the div "Gamerscore".
#2. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Here's a solution for you:


<?php
   $url = "http://gamercard.xbox.com/en-US/" . urlencode($_GET['gamertag']) . ".card";
   $url = str_replace('+','%20',$url);
   $page = fopen($url, 'r');
   $content = "";
   while( !feof( $page ) ) {
   $buffer = trim( fgets( $page, 4096 ) );
   $content .= $buffer;
   }
   
   $start = "<div id=\"Gamerscore\">";
   $end = "<\/div><\/div><div id=\"Location\">";
   
   preg_match( "/$start(.*)$end/s", $content, $match);
   $gamerscore = $match[1];
   echo $gamerscore;
?>


Obviously to get it to parse you need to go to yourfilename.php?gamertag=GAMERTAGHERE - Since it uses the GET method.
#3. Posted:
Z61
  • V5 Launch
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
UnrealEgg wrote Here's a solution for you:


<?php
   $url = "http://gamercard.xbox.com/en-US/" . urlencode($_GET['gamertag']) . ".card";
   $url = str_replace('+','%20',$url);
   $page = fopen($url, 'r');
   $content = "";
   while( !feof( $page ) ) {
   $buffer = trim( fgets( $page, 4096 ) );
   $content .= $buffer;
   }
   
   $start = "<div id=\"Gamerscore\">";
   $end = "<\/div><\/div><div id=\"Location\">";
   
   preg_match( "/$start(.*)$end/s", $content, $match);
   $gamerscore = $match[1];
   echo $gamerscore;
?>


Obviously to get it to parse you need to go to yourfilename.php?gamertag=GAMERTAGHERE - Since it uses the GET method.

Doesn't seem to work for me.
#4. Posted:
Imp
  • Retired Staff
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Z61 wrote Im trying to get the data from a div on 'Gamercard.xbox.com/{GAMERTAG}.card' but I cannot seem to get it to get the data correctly, can anyone help me?
im trying to get the text inside the div "Gamerscore".


Had a look around for you and this seemed to do the trick


$gamertag = [Enter Gamertag Here];
$url = 'http://gamercard.xbox.com/'.$gamertag.'.card';
$m= file_get_contents ($url);
$specific_div = 'Gamerscore';


preg_match_all('#<div\s*(?:id|class)\s*=\s*"'.preg_quote($specific_div).'">(.+?)</div>#is', $m, $match);

$thisGamerScore = $match[1];


And if you change the $specific_div variable, you can find other bits to ;)
#5. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Imp wrote
Z61 wrote Im trying to get the data from a div on 'Gamercard.xbox.com/{GAMERTAG}.card' but I cannot seem to get it to get the data correctly, can anyone help me?
im trying to get the text inside the div "Gamerscore".


Had a look around for you and this seemed to do the trick


$gamertag = [Enter Gamertag Here];
$url = 'http://gamercard.xbox.com/'.$gamertag.'.card';
$m= file_get_contents ($url);
$specific_div = 'Gamerscore';


preg_match_all('#<div\s*(?:id|class)\s*=\s*"'.preg_quote($specific_div).'">(.+?)</div>#is', $m, $match);

$thisGamerScore = $match[1];


And if you change the $specific_div variable, you can find other bits to ;)


The problem here is that if you use a gamertag with a space in it such as "Monkey Nuts" the URL will be invalid so you need to format the gamertag spaces to be %20. If it works for you then you must be on a newer/older PHP version since it doesn't for me.

Try this:
$gamertag = "Monkey Nuts";
$url = "http://gamercard.xbox.com/en-US/" . urlencode($gamertag) . ".card";
$url = str_replace('+','%20',$url);

$m= file_get_contents ($url);
$specific_div = 'Gamerscore';


preg_match_all('#<div\s*(?:id|class)\s*=\s*"'.preg_quote($specific_div).'">(.+?)</div>#is', $m, $match);

$thisGamerScore = $match[1];
echo $thisGamerScore[0];


Also changed Imp's URL to include /en-US/ (Can be en-GB or w/e) since it doesn't work without it when I tested it.
#6. Posted:
MrCheater
  • New Member
Status: Offline
Joined: Dec 21, 200914Year Member
Posts: 34
Reputation Power: 2
Status: Offline
Joined: Dec 21, 200914Year Member
Posts: 34
Reputation Power: 2
It would be alot better for you to use an api system,
it will be quicker, eg [ Register or Signin to view external links. ]
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.