Thanks for the comment
I have found a partial solution. This uses curl to test if the api is returning results. The downside is it calls the api twice.
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
$exists = remoteFileExists('http://api.XXX.com/a/XXX-api/xml-v2/ws-'.$number.'/q-'.$terms.'?pshid=XXX&ssty=1&cflg=r');
if (!$exists) {
echo 'no jobs returned, please try later';
} else {
$c = 0;
do {
$pFile = new SimpleXMLElement('http://api.XXX.com/a/XXX-api/xml-v2/ws-'.$number.'/q-'.$terms.'?pshid=XXX&ssty=1&cflg=r', null, true);
I tried the following suggestion from the devnetwork.net forum, and it works fine when the api is working.
However, when I change the url to a non-existent location to approximate a fail condition, the simplexml script does not return the default error message, and also seems to break page load and stop every include coming after it.
function getRemoteFile($url) {
$curl = curl_init($url);
//fetch the actual page
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//do request
$result = curl_exec($curl);
if ((false !== $result) && 200 != curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
$result = false;
}
curl_close($curl);
return $result;
}
$response = getRemoteFile('http://api.XXX.com/a/XXX-api/xml-v2/ws-'.$number.'/q-'.$terms.'?pshid=XXX&ssty=1&cflg=r');
if (false === $response) {
echo 'no jobs returned, please try later';
} else {
$pFile = new SimpleXMLElement($response);
}