从这一行得到<topalbums artist =“Adele”>

时间:2017-04-18 08:24:08

标签: php api last.fm

大家好我试图从last.fm获取信息,我正在获取专辑名称等,但我想从第2行获取。如何获得它?

<topalbums artist="Adele" page="1" perPage="1" totalPages="55092" total="55092">

^来自此处的名称,页面,每页和总页数

<lfm status="ok">
<topalbums artist="Adele" page="1" perPage="1" totalPages="55092" total="55092">
<album>

<name>21</name>
<playcount>52308837</playcount>
<mbid>c45e0e0e-48c9-4441-aac3-2f2b34202d3c</mbid>
<url>https://www.last.fm/music/Adele/21</url>
<artist>

<name>Adele</name>
<mbid>cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493</mbid>
<url>https://www.last.fm/music/Adele</url>
</artist>
<image size="small">
https://lastfm-img2.akamaized.net/i/u/34s/c894af1e6a735b9bbb2a0312c7719f40.png
</image>
<image size="medium">
https://lastfm-img2.akamaized.net/i/u/64s/c894af1e6a735b9bbb2a0312c7719f40.png
</image>
<image size="large">
https://lastfm-img2.akamaized.net/i/u/174s/c894af1e6a735b9bbb2a0312c7719f40.png
</image>
<image size="extralarge">
https://lastfm-img2.akamaized.net/i/u/300x300/c894af1e6a735b9bbb2a0312c7719f40.png
</image>
</album>
</topalbums>
</lfm>

3 个答案:

答案 0 :(得分:1)

$dom = new DOMDocument();
$dom->loadXML($xml);
$topAlbums = $dom->getElementsByTagName('topalbums')->item(0);
$artist = $topAlbums->getAttribute('artist');
echo $artist; // outputs Adele

答案 1 :(得分:0)

一种基本方法是使用正则表达式:

preg_match('/artist="([^"]*)"\\s*page="([^"]*)"\\s*perPage="([^"]*)"\\s*totalPages="([^"]*)"/', $lastfm_input, $result);
# $result[1] = name value
# $result[2] = page value
# $result[3] = perPage value
# $result[4] = totalPages value

如果属性的顺序(如perPage)发生变化,则必须使用多个正则表达式。

答案 2 :(得分:-1)

伙计们,我找到了最佳解决方案

Resources: 
  AWSEBAutoScalingGroup: 
    Metadata: 
      ? "AWS::CloudFormation::Authentication"
      : 
        S3Auth: 
          buckets: 
            - <my-bucket>
          roleName: 
            ? "Fn::GetOptionSetting"
            : 
              DefaultValue: aws-elasticbeanstalk-ec2-role
              Namespace: "aws:asg:launchconfiguration"
              OptionName: IamInstanceProfile
          type: s3
files: 
  /root/.ssh/github-eb-key: 
    authentication: S3Auth
    mode: "000600"
    owner: root
    group: root
    source: "https://s3-eu-west-1.amazonaws.com/<my-bucket>/github-eb-key"
  /root/.ssh/config: 
    mode: "000600"
    owner: root
    group: root
    content: |
      Host github.com
        IdentityFile /root/.ssh/github-eb-key
        IdentitiesOnly yes
        UserKnownHostsFile=/dev/null
        StrictHostKeyChecking no
相关问题