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:
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".
im trying to get the text inside the div "Gamerscore".
#2. Posted:
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:
Obviously to get it to parse you need to go to yourfilename.php?gamertag=GAMERTAGHERE - Since it uses the GET method.
<?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.
- 0useful
- 0not useful
#3. Posted:
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.
- 0useful
- 0not useful
#4. Posted:
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 ;)
- 0useful
- 0not useful
#5. Posted:
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 wroteZ61 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.
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: Dec 21, 200914Year Member
Posts: 34
Reputation Power: 2
Status: Offline
Joined: Dec 21, 200914Year Member
Posts: 34
Reputation Power: 2
You are viewing our Forum Archives. To view or take place in current topics click here.