Jump to content

Why is it only echoing one id?


devWhiz

Recommended Posts


$lastfm = file_get_contents('http://www.last.fm/group/Rishloo/members');
$users = explode('" id="r4_', $lastfm);
$users = explode('">', $users[1]);
var_dump($users[0]);

 

that is only echoing one ID

 

http://www.last.fm/group/Rishloo/members

 

1 id for each member it should be echoing.. any idea why it isnt?

 


$lastfm = file_get_contents('http://www.last.fm/group/Rishloo/members');
$grab_id=explode('" id="r4_', $lastfm);
for($b=1; $b<count($grab_id); $b++){
$getid=explode('">', $grab_id[$b]);
echo count($getid[0]);
}

 

that echos all 1's.. any help is appreciated.. thanks

 

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/234995-why-is-it-only-echoing-one-id/
Share on other sites

I assumed you want more than just the IDs. The following code will provide the id, image url, name(s), bio for each user, plus their listening or last track info.

$lastfm = file_get_contents('http://www.last.fm/group/Rishloo/members');

$pattern = array();
$patterns[] = "id=\"r4_([^\"]*)\"";
$patterns[] = "class=\"userImage\"><img.*?src=\"([^\"]*)\"";
$patterns[] = "<\/span> ([^<]*)<";
$patterns[] = "<strong class=\"fn\">([^<]*)<";
$patterns[] = ",             ([^\n]*)";
$patterns[] = "(Listening|Last track):";
$patterns[] = "<a href=\"\/music\/[^\"]*\">([^<]*)<";
$patterns[] = "<a href=\"\/music\/[^\"]*\">([^<]*)<";

$pattern = "#" . implode('.*?', $patterns) . "#sim";
preg_match_all($pattern, $lastfm, $matches, PREG_SET_ORDER);

//Remove first element from each record, it contains HTML code that will mess up the output
//This is not needed and only included so I can print_r the results to the page
foreach($matches as $idx => $value)
{
    unset($matches[$idx][0]);
}

echo "<pre>\n";
print_r($matches)

 

This is probably not the most efficient way to get that data, but it works.

 

Here is some of the output from the above:

Array
(
    [0] => Array
        (
            [1] => 20770702
            [2] => http://userserve-ak.last.fm/serve/64s/60856343.jpg
            [3] => Alice_in_pains
            [4] => nermin
            [5] => 20, Female, Azerbaijan
            [6] => Last track
            [7] => Tool
            [8] => Useful Idiot
        )

    [1] => Array
        (
            [1] => 35133964
            [2] => http://userserve-ak.last.fm/serve/64s/55291785.jpg
            [3] => agostinoo
            [4] => Agostino
            [5] => 19, Male, United Kingdom
            [6] => Last track
            [7] => Cat Power
            [8] => Fool
        )

    [2] => Array
        (
            [1] => 38461866
            [2] => http://userserve-ak.last.fm/serve/64s/61233861.jpg
            [3] => maverikgameface
            [4] => Reece Gerard
            [5] => 20, United States
            [6] => Last track
            [7] => A Perfect Circle
            [8] => Weak And Powerless
        )

OK, after some further verification it looks like the above expressions were preventing some records to be properly identified. I don't have the time to evaluate all the records to determine the problem. Just comment out the lines identified below and you should get the ID, image URL and name of each user. I can't guarantee it will always work based upon how that website modifies their data

$pattern = array();
$patterns[] = "id=\"r4_([^\"]*)\"";
$patterns[] = "class=\"userImage\"><img.*?src=\"([^\"]*)\"";
$patterns[] = "<\/span> ([^<]*)<";
//$patterns[] = "<strong class=\"fn\">([^<]*)<";
//$patterns[] = ",             ([^\n]*)";
//$patterns[] = "(Listening|Last track):";
//$patterns[] = "<a href=\"\/music\/[^\"]*\">([^<]*)<";
//$patterns[] = "<a href=\"\/music\/[^\"]*\">([^<]*)<";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.