Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple xml

Status
Not open for further replies.

mwpclark

Programmer
Mar 14, 2005
59
US
Hi

I am working with php5 simplexml to grab feeds from the simplyhired job posting affiliate program.

The script grabs the xml feed with this string:

[blue]$pFile = new SimpleXMLElement('url-with-variables', null, true);[/blue]

It works very well and generally the response from the xml server is fast. The server on which the script executes and serves the pages is fast with good connectivity.

However sometimes there is some hangup or delay, and it leaves a blank spot on the web page. When I reload the web page the blank spot is filled with job postings, like it should be. I have no idea how many viewers are seeing blank spots rather than job postings.

I am wondering if there is an additional command I could insert to force the load, or force a reload if blank, or force a wait for response, or set a timeout limit.

Thanks
Mike
 
how about looping it?

Code:
$c = 0;
do {
 $pFile = new SimpleXMLElement('url-with-variables', null, true); 
 $c++;
} while ( (empty($pFile) || $pFile === false) && $c < 20)
 
Thanks, I added this snippet to the script (with a final semicolon). The script still functions, didn't break it, heh...

I will monitor and see if I ever there are any blank spots or hangups in the future.

Interesting, just learned the do-while loop, a bit different from while.

Cheers
Mike
 
you might want to add this in too

Code:
libxml_use_internal_errors(true);
$errors = array();
$isError = false;
do {
 $pFile = new SimpleXMLElement('url-with-variables', null, true);
 if (!$pFile){
  foreach(libxml_get_errors() as $error) {
    $errors[] = $error->message;
  }
  $isError = true;
 }
 $c++;
} while ( (empty($pFile) || $pFile === false) && $c < 20);
if ($isError){
  //do something with errors
  echo "<pre>".print_r($errors, true) . "</pre>";
}
 
Thanks, if I see any further instances of blank space or non-written imports, I will do something like this and write to a log file. But the results have generally been very good and very dependable. Also I am personally a long way from any US backbone (Argentina) and some things are hard to test with unreliable connectivity.

This feed has error reporting codes which I use to generate specific error messages that are printed to web. However, I think that may be an "external" error. I don't particularly want to notify the viewer that there is an internal error or problem with a feed that is displayed as web page text.

In a separate script, which access the same api and displays the results of a viewer-activated keyword search, I use this error reporting:

$error = $pFile->error;

if ($error != "") {
$error_type = $pFile->error["type"];
$error_code = $pFile->error["code"];
if ($error_code == "1.0") { echo '<br /><br /><h3>No search term entered. All queries must, at a minimum, contain a location or a keyword.</h3>';}
if ($error_code == "1.1") { echo '<br /><br /><h3>No results for this query. Please modify your search.</h3>';}
if ($error_code == "1.2") { echo '<br /><br /><h3>No jobs found in location. Location was found, but zero jobs are available.</h3>';}
if ($error_code == "1.3") { echo '<br /><br /><h3>Keyword not found. Keyword not found in our index.</h3>';}
if ($error_code == "1.4") { echo '<br /><br /><h3>Location not found. Location not found in our database of US locations.</h3>';}
if ($error_code == "1.5") { echo '<br /><br /><h3>Multiple possible locations. Location needs to be clarified to determine search location.</h3><br /><br /><ul style="font-size:12px;">';
foreach ($pFile->error->option as $pChild)
{
$option_url = $pChild["url"];
$option_text = $pChild;
$option_url = str_replace('/a/jobs-api/xml_v2/','/jobs/', $option_url);
echo '<li><a href="'.$option_url.'">'.$option_text.'</a><br />';
}
echo '</ul>';
}
}
 
i was suggesting the script purely as a way in which you could see what errors in the xml result you were getting.

you might consider getting the data using cURL and then parsing the string using simplexml. the cURL library gives you more options for debugging and controlling the connection.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top