我可以从响应中轻松地从api中提取所需的节点:https://musicbrainz.org/ws/2/release-group/fc02b10c-8a09-38d6-b612-22c47794d2c6?inc=releases+media+url-rels
但是,我试图从下面得到关系类型discogs的目标没有运气,任何帮助赞赏
<relation-list target-type="url">
<relation type-id="6578f0e9-1ace-4095-9de8-6e517ddb1ceb" type="wikipedia">
<target id="cba6f347-45b3-4bd5-8b7f-818fa76a63b2">
http://en.wikipedia.org/wiki/Insineratehymn
</target>
</relation>
<relation type-id="99e550f3-5ab4-3110-b5b9-fe01d970b126" type="discogs">
<target id="e9e6e840-ca1a-47fa-b835-47d0d86bcda8">
http://www.discogs.com/master/316106
</target>
</relation>
<relation type-id="b988d08c-5d86-4a57-9557-c83b399e3580" type="wikidata">
<target id="f9b00d41-6f25-4174-9c72-ba1c452cfa6d">
http://www.wikidata.org/wiki/Q1932481
</target>
</relation>
</relation-list>
答案 0 :(得分:1)
检查
<?php
$url = 'https://musicbrainz.org/ws/2/release-group/fc02b10c-8a09-38d6-b612-22c47794d2c6?inc=releases+media+url-rels';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result= curl_exec ($ch);
curl_close ($ch);
$res = simplexml_load_string($result);
//echo "<pre>";
//print_r($res);
$listing = $res->{'release-group'}->{'relation-list'}->relation;
// full listing
foreach($listing as $list)
{
echo $list['type']."<br>"; // full listing
}
//Particular url
foreach($listing as $list)
{
if($list['type'] == 'discogs')
{
echo $list->target; // Particular url
}
}
exit;
<强>输出强>
wikipedia
discogs
wikidata
http://www.discogs.com/master/316106
答案 1 :(得分:1)
您可以使用->attributes()
方法检查该特定标记是否包含所需的属性。例如:
// without xpath
$xml = simplexml_load_file('https://musicbrainz.org/ws/2/release-group/fc02b10c-8a09-38d6-b612-22c47794d2c6?inc=releases+media+url-rels');
foreach($xml->{'release-group'}->{'relation-list'}->{'relation'} as $relation) {
if($relation->attributes()['type'] == 'discogs') { // dereference (5.4 or above)
echo (string) $relation->target; // http://www.discogs.com/master/316106
}
}
或者您也可以使用xpath并在xml中注册命名空间,并直接将其作为目标:
$xml->registerXPathNamespace('r', 'http://musicbrainz.org/ns/mmd-2.0#'); // registers xmlns (namespace)
$discogs = (string) $xml->xpath('//r:release-group/r:relation-list/r:relation[@type="discogs"]/r:target')[0];
echo $discogs; // http://www.discogs.com/master/316106
答案 2 :(得分:1)
刚刚制作了一个简单的代码:
$xml = simplexml_load_file('https://musicbrainz.org/ws/2/release-group/fc02b10c-8a09-38d6-b612-22c47794d2c6?inc=releases+media+url-rels');
foreach($xml->{'release-group'}->{'relation-list'}->relation as $relation) {
if($relation['type'] == 'discogs') {
$link = $relation->target;
}
}
echo $link;
输出:
http://www.discogs.com/master/316106
答案 3 :(得分:0)
XML使用命名空间。 DOM + Xpath允许您考虑到这一点并直接获取字符串值:
$dom = new DOMDocument();
$dom->load(
'https://musicbrainz.org/ws/2/release-group/fc02b10c-8a09-38d6-b612-22c47794d2c6?inc=releases+media+url-rels'
);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('mb', "http://musicbrainz.org/ns/mmd-2.0#");
$expression = 'string(
//mb:release-group
/mb:relation-list
/mb:relation[@type = "discogs"]
/mb:target
)';
var_dump($xpath->evaluate($expression));
输出:
string(36) "http://www.discogs.com/master/316106"