Reputation: 1071
I have an XML document that will either return a URL or an error code. If it returns an error code I'd like to redirect the page to an error image. I can get SimpleXML to return the URL, but I am not sure how to write a condition if the error returns. If anyone has any suggestions, that'd be great! This is what I have right now:
<?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));
?>
This is what the XML document returns if it cannot be found:
<?xml version="1.0" encoding="utf-8"?>
<lookup status="failed">
<error code="3">Art not found</error></lookup>
Upvotes: 2
Views: 303
Reputation: 42458
How about checking for the error node in your XML, and if you find it then handling the various errors? If you don't find it you can continue with your normal logic.
if (isset($xml->error)) {
switch ($xml->error['code']) {
case '3':
// not found stuff here
break;
// other error codes here
}
} else {
// success logic here
$img = $xml->xpath('//image[@size="large"]');
$large = (string) $img[0];
header("Location: " . urldecode($large));
}
Upvotes: 4