【问题标题】:Simple XML from string - what's missing?来自字符串的简单 XML - 缺少什么?
【发布时间】:2013-06-24 10:03:40
【问题描述】:

我在这里让自己发疯,我几乎可以肯定我至少在正确的轨道上。

我只是试图解析从以 XML 形式返回的 API 获得的响应。我真的只需要打印“歌词”索引。

任何人,这是代码:

<?php
    $artist = $_GET['artist'];
    $song = $_GET['song'];

    if(isset($_GET['artist']) && isset($_GET['song']))
    {
        $result = get_lyrics($artist, $song);
    } else {
        $result = "";
    }

    function get_lyrics($artist, $song)
    {
        $postURL = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=".urlencode($artist)."&song=".urlencode($song);
        echo $postURL;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postURL);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

?>


<html>
    <head><title>Lyric Search</title></head>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="get">
        <p>Artist<input type="input" name="artist" /></p>
        <p>Song<input type="input" name="song" /></p>
        <input type="submit" value="submit" />
    </form>

    <div id="results">
        <?php

            $xml = simplexml_load_string($result);

            foreach($xml->GetLyricResult as $lyric)
            {
                echo $lyric->Lyric;
            }
        ?>
    </div>
</html>

这里是 xml....

http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=lady+gaga&song=poker+face

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    你不需要使用foreach,因为只有一个歌词。

    你只需要:

    <div id="results">
    <?php
    $xml = simplexml_load_string($result);
    echo $xml->Lyric[0];
    ?>
    </div>
    

    【讨论】:

      【解决方案2】:

      试试:

      <html>
          <head><title>Lyric Search</title></head>
      
          <form action="<?=$_SERVER['PHP_SELF']?>" method="get">
              <p>Artist<input type="input" name="artist" /></p>
              <p>Song<input type="input" name="song" /></p>
              <input type="submit" value="submit" />
          </form>
      
          <div id="results">
      <?php
          if(isset($_GET['artist']) && isset($_GET['song'])){
              $result = get_lyrics($_GET['artist'],$_GET['song']);
              $xml = simplexml_load_string($result);
      
              echo "<pre>";
              //print_r($xml);
              echo $xml->Lyric;
              echo "</pre>";
          }
      
          function get_lyrics($artist, $song)
          {
              $postURL = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=".urlencode($artist)."&song=".urlencode($song);
              echo $postURL;
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $postURL);
              curl_setopt($ch, CURLOPT_HEADER, 0);
              curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
              $result = curl_exec($ch);
              curl_close($ch);
      
              return $result;
          }
      ?>
          </div>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        • 1970-01-01
        相关资源
        最近更新 更多