[PHP] Data vanuit XML bestand weergeven

Pagina: 1
Acties:
  • 142 views sinds 30-01-2008
  • Reageer

Onderwerpen


Acties:
  • 0 Henk 'm!

  • kleautviool
  • Registratie: Mei 2003
  • Laatst online: 17-09 15:43
Ik heb een vreemd probleem. Ik wil dus d.m.v. PHP mijn weekly artist feed van last.fm op mijn site weergeven. Nu heb ik op internet een artikel gevonden wat dit doet alleen dan geeft het betreffende script maar 2 dingen weer.

Nu dacht ik het wel te begrijpen en heb ik wat aanpassingen gemaakt. Momenteel heb ik onderstaande code:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
  $xml_file  = "http://ws.audioscrobbler.com/1.0/user/Maurice-k/weeklyartistchart.xml";

  $xml_artist_name_key = "*WEEKLYARTISTCHART*ARTIST*NAME";
  $xml_artist_play_key = "*WEEKLYARTISTCHART*ARTIST*PLAYCOUNT";
  $xml_artist_url_key = "*WEEKLYARTISTCHART*ARTIST*URL";

  $story_array = array();

  $counter = 0;
  class xml_story{
    var $name, $play, $url;
  }

  function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
  }

  function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
  }

  function contents($parser, $data){
    global $current_tag, $xml_artist_name_key, $xml_artist_play_key, $xml_artist_url_key, $counter, $story_array;
    switch($current_tag){
      case $xml_artist_name_key:
        $story_array[$counter] = new xml_story();
        $story_array[$counter]->name = $data;
      break;
      case $xml_artist_play_key:
        $story_array[$counter]->playcount = $data;
        $counter++;
      break;
      case $xml_artist_url_key:
        $story_array[$counter]->url = $data;
        $counter++;
      break;
    }
  }

  $xml_parser = xml_parser_create();

  xml_set_element_handler($xml_parser, "startTag", "endTag");

  xml_set_character_data_handler($xml_parser, "contents");

  $fp = fopen($xml_file, "r") or die("Could not open file");

  $data = fread($fp, 4096) or die("Could not read file");

  if(!(xml_parse($xml_parser, $data))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser));
  }

  xml_parser_free($xml_parser);

  fclose($fp); 

  echo("<ol>\n");
  for($x=0;$x<10;$x++){
    echo("<li><a href=\"".$story_array[$x]->url."\" title=\"".$story_array[$x]->name." &ndash; Music at Last.fm\">".$story_array[$x]->name."</a> - ".$story_array[$x]->playcount."</li>\n");
  } 
?>
</ol>

Alleen nu is het vreemde dat ik de volgende output krijg:
HTML:
1
2
3
4
5
6
7
8
9
10
11
12
<ol>
<li><a href="" title="Juelz Santana &ndash; Music at Last.fm">Juelz Santana</a> - 50</li>
<li><a href="http://www.last.fm/music/Juelz+Santana" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="The Game &ndash; Music at Last.fm">The Game</a> - 38</li>
<li><a href="http://www.last.fm/music/The+Game" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="édéric Chopin &ndash; Music at Last.fm">édéric Chopin</a> - 34</li>
<li><a href="http://www.last.fm/music/Fr%C3%A9d%C3%A9ric+Chopin" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="Chamillionaire &ndash; Music at Last.fm">Chamillionaire</a> - 28</li>
<li><a href="http://www.last.fm/music/Chamillionaire" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="T.I. &ndash; Music at Last.fm">T.I.</a> - 27</li>
<li><a href="http://www.last.fm/music/T.I." title=" &ndash; Music at Last.fm"></a> - </li>
</ol>


Ik heb mijn code 100 keer doorgelezen, maar zie gewoon niet wat ik fout doe. Misschien iemand die ziet waar ik nu de mist in ga?

Acties:
  • 0 Henk 'm!

  • Spockz
  • Registratie: Augustus 2003
  • Laatst online: 10:08

Spockz

Live and Let Live

En wat moet het worden? Ik denk dat je die HTML entities eruit wilt hebben?

Kijk dan eens hier.

(bovendien is die opzet met globals ook niet echt charmant.)
En welke versie van php gebruik je trouwens? Als je PHP5 gebruikt is simplexml misschien wel iets. :)

[ Voor 33% gewijzigd door Spockz op 30-03-2007 17:33 ]

C'est le ton qui fait la musique. | Blog | @linkedin
R8 | 18-55 IS | 50mm 1.8 2 | 70-200 2.8 APO EX HSM | 85 1.8


Acties:
  • 0 Henk 'm!

Verwijderd

Je update je array counter 2x per compleet data element :)

[ Voor 10% gewijzigd door Verwijderd op 30-03-2007 17:32 ]


Acties:
  • 0 Henk 'm!

  • kleautviool
  • Registratie: Mei 2003
  • Laatst online: 17-09 15:43
:X |:(

Dat was hem idd :D

Dank Dank dank _/-\o_ continu overheengekeken

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  function contents($parser, $data){
    global $current_tag, $xml_artist_name_key, $xml_artist_play_key, $xml_artist_url_key, $counter, $story_array;
    switch($current_tag){
      case $xml_artist_name_key:
        $story_array[$counter] = new xml_story();
        $story_array[$counter]->name = $data;
      break;
      case $xml_artist_play_key:
        $story_array[$counter]->playcount = $data;
      break;
      case $xml_artist_url_key:
        $story_array[$counter]->url = $data;
        $counter++;
      break;
    }
  }

[ Voor 78% gewijzigd door kleautviool op 30-03-2007 17:37 ]