我有一個XML文檔,將返回一個URL或錯誤代碼。如果它返回一個錯誤代碼,我想將頁面重定向到一個錯誤圖像。我可以讓SimpleXML返回URL,但如果錯誤返回,我不確定如何編寫條件。如果有人有任何建議,那會很棒!這就是我現在所擁有的:SimpleXML如果條件
<?php
error_reporting(0);
$url = 'http://radiocast.co/art/api1/key=KeyHere&album=' . htmlspecialchars($_GET["album"]) . '&artist=' . htmlspecialchars($_GET["artist"]) . '';
$xml = simplexml_load_file($url);
$img = $xml->xpath('//image[@size="large"]');
$large = (string)$img[0];
header("Location:".urldecode($large));
?>
這是什麼,如果它不能找到的XML文檔的回報:
<?xml version="1.0" encoding="utf-8"?>
<lookup status="failed">
<error code="3">Art not found</error></lookup>
提示1:在根元素的名稱後面將變量重命名爲'$ lookup'。提高可讀性。技巧2:http_build_url或sprintf更適合構建url – Gordon 2012-02-14 22:33:26