1

I will first explain what I want the code to do.

The code should show the data of a player in a game through the use of an api provided by the game itself (http://services.runescape.com/m=hiscore/index_lite.ws?player=NameOfPlayer). On my website, each player has its own page, and the title of the page is the name of the player. Consequently, I would like to grab the title of the page in order to show the player's data under it.

(The other part of the code is fully working. If I remove the $title line and change the $url to the one of a player it works).

I am having trouble to use the code below. When I insert this code into a page, the page breaks. But, if I remove the $title = alert(document.title);'); line, the page does not break.

I did some research online and on stackoverflow, and I tried to change that line in str_get_html('<title></title>'); but it didn't help.

<?php
$title = str_get_html('<title></title>');
$url = "http://services.runescape.com/m=hiscore/index_lite.ws?player=$title";
$f = file_get_contents($url);
$items = explode(' ', $f);
foreach($items as $item){
    $player = explode(",", $item);
    echo "Total Level: $player[1]<br />";
    echo "Attack: $player[3]<br />";
    echo "Defence: $player[5]<br />";
    echo "Strength: $player[7]<br />";
    echo "Constitution: $player[9]<br />";
    echo "Ranged: $player[11]<br />";
    echo "Prayer: $player[13]<br />";
    echo "Magic: $player[15]<br />";
    echo "Cooking: $player[17]<br />";
    echo "Woodcutting: $player[19]<br />";
    echo "Fletching: $player[21]<br />";
    echo "Fishing: $player[23]<br />";
    echo "Firemaking: $player[25]<br />";
    echo "Crafting: $player[27]<br />";
    echo "Smithing: $player[29]<br />";
    echo "Mining: $player[31]<br />";
    echo "Herblore: $player[33]<br />";
    echo "Agility: $player[35]<br />";
    echo "Thieving: $player[37]<br />";
    echo "Slayer: $player[39]<br />";
    echo "Farming: $player[41]<br />";
    echo "Runecrafting: $player[43]<br />";
    echo "Hunter: $player[45]<br />";
    echo "Construction: $player[47]<br />";
    echo "Summoning: $player[49]<br />";
    echo "Dungeoneering: $player[51]<br />";
    echo "Divination: $player[53]<br />";
    echo "Invention: $player[55]<br />";
}
?>

I was also thinking that it was possible to grab the data in another way, but I have no idea on how to do it. The website runs on WordPress, and each player page is connected to the WordPress account of a registered user. It is possible to grab the data of a user by using the following reference:

get_userdata( $userid );

Each user has its player name stored as "nickname". Consequently, it is possible to grab the player name of each user by using:

<?php $user_info = get_userdata(1);
  echo $user_info->nickname;
?>

However, the user-pages are generated by a plugin. I have tried to tweak the code inside it, but I couldn't manage to do it successfully.

Do you know how I can grab the title of the page without breaking it, or grab the nickname of a user?

I have now set the pages to look like: example.com/user/playername (where only playername changes). I am now using: global $post; $pagename = $post->post_name; However, $pagename is shown as user instead of playername. Do you know how I can make it "get" the playername instead of its slug?

EDIT 2:

function getPath($url)
{
$path = parse_url($url,PHP_URL_PATH);
$lastSlash = strrpos($path,"/");
return substr($path,1,$lastSlash-1);
}
Nox19
  • 35
  • 9

1 Answers1

3

You could try parsing the HTML returned by the $url using this function

<?php
    function page_title($url) {
        $fp = file_get_contents($url);
        if (!$fp) 
            return null;

        $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
        if (!$res) 
            return null; 

        // Clean up title: remove EOL's and excessive whitespace.
        $title = preg_replace('/\s+/', ' ', $title_matches[1]);
        $title = trim($title);
        return $title;
    }
?>

You can find more details in this answer.

EDIT

Since you are looking to find the player name from your own URL you can use get_permalink().

Then you need to extract the player name from a URL like https://example.com/user/eibe/.

To do that you can use the following code:

<?php

$permalink = get_permalink();  // $permalink is now 'https://example.com/user/eibe/'

$permalink = trim($permalink, "/"); // $permalink is now 'https://example.com/user/eibe'

$player_name = substr($permalink, strrpos($permalink, '/') + 1); // $player_name is now 'eibe'

?>

Hope this helps.

anpel
  • 931
  • 6
  • 16
  • I have tried the code and it doesn't work,,, I have now set the pages to look like: `example.com/user/playername` (where only playername changes). I am now using: `global $post; $pagename = $post->post_name;` However, $pagename is shown as `user` instead of `playername`. Do you know how I can make it "get" the `playername` instead of its slug? – Nox19 Jan 31 '16 at 20:07
  • So you need to get the title of your current wordpress page and not the one on `"http://services.runescape.com/m=hiscore/index_lite.ws?player=$title"`? If so, you could try using [get_the_title()](https://developer.wordpress.org/reference/functions/get_the_title/) – anpel Jan 31 '16 at 20:16
  • Yes, I have to get the title of the current page. I have already tried the `get_the_title()` function, but it didn't work. That is probably because the page is set to be 'User' and the `playername` is only an id of the page. Consequently, I think that the real url is `example.com/user.php?id=playername` but is shown as `example.com/user/playername`. – Nox19 Jan 31 '16 at 20:21
  • Then how about trying [get_permalink()](https://developer.wordpress.org/reference/functions/get_permalink/) to get the permalink as `$str` and then using `substr($str, strrpos($str, '/') + 1);` to keep only the part after the last '/' in it? For more details on the last part you can check [here](http://stackoverflow.com/a/17030851/5827913) – anpel Jan 31 '16 at 20:34
  • I coudln't fully understand what you said (I am a beginner). Consequently, I have adapted the code you sent me: `see function above after "EDIT 2` Now if I do `echo getPath("https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);` I get `user/playername`. I guess I did something wrong with the substr... And do you know how I could put the "result" inside the link? (`"http://services.runescape.com/m=hiscore/index_lite.ws?player=$last"` where $last is where I would like to put the link). – Nox19 Jan 31 '16 at 21:06
  • I was suggesting this: `$str = get_permalink();` and then `$player_name = substr($str, strrpos($str, '/') + 1);`. Does it give you the player name? – anpel Jan 31 '16 at 21:09
  • It gives me as a result `0` while the player name is `eibe` – Nox19 Jan 31 '16 at 21:17
  • What do you get if you just use `exit(get_permalink());` while the player name is Eibe? – anpel Jan 31 '16 at 21:18
  • If I set `str = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];` I still get 0. If I set `str = exit(get_permalink());` I get `https://example.com/user/` – Nox19 Jan 31 '16 at 21:21
  • I have updated my answer. Please check it and let me know. – anpel Jan 31 '16 at 21:32
  • Sorry I didn't explain the comment well enough. It shows `https://example.com/user/` while the real path is `https://example.com/user/eibe/` (and I need to get eibe, which is after user) – Nox19 Jan 31 '16 at 21:34
  • Please check [this sandbox](http://sandbox.onlinephpfunctions.com/code/d175bbf814ff53555ac92ff5d3a2c19b5de3c7fe) – anpel Jan 31 '16 at 21:37
  • 2
    It is working! I know I shouldn't comment to just to say "Thank you" but I have to since I made you spend a lot of time on this... Thank you very much! – Nox19 Jan 31 '16 at 21:43