Catching PHP errors if XML file is empty - php

so I'm grabbing some information from an XML file like so:
$url = "http://myurl.blah";
$xml = simplexml_load_file($url);
Except sometimes the XML file is empty and I need the code to fail gracefully but I can't seem to figure out how to catch the PHP error. I tried this:
if(isset(simplexml_load_file($url)));
{
$xml = simplexml_load_file($url);
/*rest of code using $xml*/
}
else {
echo "No info avilable.";
}
But it doesn't work. I guess you can't use ISSET that way. Anyone know how to catch the error?

$xml = file_get_contents("http://myurl.blah");
if (trim($xml) == '') {
die('No content');
}
$xml = simplexml_load_string($xml);
Or, possibly slightly more efficient, but not necessarily recommended because it silences errors:
$xml = #simplexml_load_file($url);
if (!$xml) {
die('error');
}

Don't use isset here.
// Shutdown errors (I know it's bad)
$xml = #simplexml_load_file($url);
// Check you have fetch a response
if (false !== $xml); {
//rest of code using $xml
} else {
echo "No info avilable.";
}

if (($xml = simplexml_load_file($url)) !== false) {
// Everything is OK. Use $xml object.
} else {
// Something has gone wrong!
}

From PHP manual, error handling (click here):
var_dump(libxml_use_internal_errors(true));
// load the document
$doc = new DOMDocument;
if (!$doc->load('file.xml')) {
foreach (libxml_get_errors() as $error) {
// handle errors here
}
libxml_clear_errors();
}

Related

How to add an error handling to read an XML file in php?

I am developing a PHP script that allows me to modify tags in an XML file and move them once done.
My script works correctly but I would like to add error handling: So that if the result of my SQL query does not return anything display an error message or better, send a mail, and not move the file with the error and move to the next.
I did some tests but the code never displays the error and it moves the file anyway.
Can someone help me to understand why? Thanks
<?php
}
}
$xml->formatOutput = true;
$xml->save($source_file);
rename($source_file,$destination_file);
}
}
closedir($dir);
?>
Give this one a try
$result = odbc_fetch_array($exec);
if ($result === false || $result['GEAN'] === null) {
echo "GEAN not found for $SKU_CODE";
// continue;
}
$barcode = (string) $result['GEAN'];
echo $barcode; echo "<br>"; //9353970875729
$node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
$node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result[GEAN]));

php foreach - try catch, I don't know why I get 500 Error. I have call activeMQ

php foreach - try catch, I don't know why I get 500 Error.
in my code, I used this way to call activeMQ, but I will get 500 Error. I don't know what's happen.
This code only run to $url = "tcp://".$url; cannot run to uder try{}. Where is issues?
$stomp = null;
$mqUser='aaa';
$mqPasswd='aaa';
foreach ($urls as $url) {
$url = "tcp://".$url;
//echo $url." stomp status: null;<br />";
try {
$stomp = new Stomp($url);
} catch(Exception $e) {
}
if ($stomp) {
break;
}
}

loadHTMLFile loads, but is empty? PHP

so I tried to get a fix for this earlier but I think we were all going in the wrong direction. I'm trying to check two servers to make sure that at least one of them are active to make a call to. The service provides me with a page for each that simply has "OK" under a div with id="server_status". When I try to loadHTMLFile into a variable, it returns true, but I can never pull the element I need from it. After doing some output testing with saveHTML(), it appears that the variable holding the DOMDocument is empty. Here's my code:
servers = array('tpeweb.paybox.com', // primary URL
'tpeweb1.paybox.com'); // backup URL
foreach($servers as $server){
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$docText = $doc->saveHTML();
if($doc) {
echo "HTML should output here: ";
echo $docText;
}
if(!$doc) {
echo "HTML file not loaded";
}
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
return array(true, 'https://'.$server.'/cgi/MYchoix_pagepaiement.cgi');
}
}
return array(false, 'e404.html');
All I get as output is "HTML should output here: " twice, and then it returns the array at the bottom. This is the code that they provided:
$servers = array('tpeweb.paybox.com', // primary URL
'tpeweb1.paybox.com'); // backup URL
$serverOK = "";
foreach($servers as $server){
$doc = new DOMDocument();
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
$serverOK = $server;
break;
}
// else : Server is up but services are not available .
}
if(!$serverOK){
die("Error : no server found");
}
echo 'Connecting to https://'.$server.'/cgi/MYchoix_pagepaiement.cgi';
This also seems to be having the same problem. Could it be something with my PHP configuration? I'm on version 5.3.6.
Thanks,
Adrian
EDIT:
I tried it by inputting the HTML as a string instead of calling it to the server and it worked fine. However, calling the HTML into a string to use in the PHP function results in the same issue. Fixes??

Add http or https for user input validation

$xml = $_GET['url']
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
..
..
if the user put without http or https my script will be broken, is concatenation a good way to validation in this case?
The simplest way of doing this is checking for the presence of http:// or https:// at the beginning of the string.
if (preg_match('/^http(s)?:\/\//', $xml, $matches) === 1) {
if ($matches[1] === 's') {
// it's https
} else {
// it's http
}
} else {
// there is neither http nor https at the beginning
}
You are using a get method. Or this is done by AJAX, or the user appends a url in the querystring You are not posting a form?
Concatenation isn't going to cut it, when the url is faulty. You need to check for this.
You can put an input with placeholder on the page, to "force" the user to use http://. This should be the way to go in HTML5.
<input type="text" pattern="^(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$" placeholder="http://" title="URLs need to be proceeded by http:// or https://" >
This should check and forgive some errors. If an url isn't up to spec this will return an error, as it should. The user should revise his url.
$xml = $_GET['url']
$xmlDoc = new DOMDocument();
if (!preg_match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, $xml ) )
{
echo 'This url is not valid.';
exit;
}
else if (!preg_match(/^http(s)?:\/\/, $xml))
{
//no http present
$orgUrl = $xml;
$xml = "http://".$orgUrl;
//extended to cope with https://
$loaded = loadXML();
if (substr($loaded, 0, 5) == "false")
{
//this attempt failed.
$xml = "https://".$orgUrl;
$loaded = loadXML();
if (substr($loaded, 0, 5) == "false")
{
echo substr($loaded, 6);
exit;
}
}
}
else
{
$loaded = loadXML();
}
function loadXML()
{
try {
return $xmlDoc->load($xml);
}
catch($Ex)
{
return echo 'false Your url could\'t be retrieved. Are you sure you\'ve entered it correctly?';
}
}
You can also use curl to check the url before loading xml:
$ch = curl_init($xml);
// Send request
curl_exec($ch);
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "$error_message :: while loading url";
}
// Close the handle
curl_close($ch);
Important side-note: Using this methods to check if the url is available and than take the appropriate action can take a very long time, since the server response can take a while to return.

Warnings when file_get_content wrong url

I have this code:
<?php
$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);
if(preg_match("/<title>(.+)<\/title>/i",$file,$m))
print "$m[1]";
else
print "The page doesn't have a title tag";
?>
It works fine when the url is a proper url, but when I put in nonsense then I get two warning messages:
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Navn eller tjeneste ukendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4
Warning: file_get_contents(http://asdsfsfsfsfsdfad.com) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Navn eller tjeneste ukendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4
Any way to prevent this?
The easiest solution would be to just suppress the error:
echo #file_get_contents("http://asdsfsfsfsfsdfad.com");
However, error suppression is generally considered bad practise because you never know what went wrong, so it is better to have a handler that selectively handles errors, for instance
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
echo file_get_contents("http://asdsfsfsfsfsdfad.com");
This would suppress any E_WARNINGS with a message containing 'php_network_getaddresses'. Any other Warnings will not be suppressed.
In addition, you dont want Regex to parse HTML, but use an HTML Parser, like one of those given in
How do you parse and process HTML/XML in PHP?
So you could do it with DOM. Again, either using Error Suppression (bad)
$dom = new DOMDocument;
#$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $dom->nodeValue : 'No Title found';
Or selectively suppressing network errors:
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue : 'No Title found';
However, this will then result in parsing errors because loadHTMLFile will not return any HTML, so to suppress the parsing errors as well, you'd have to do:
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
libxml_clear_errors();
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue : 'No Title found';
implode() expects the second parmeter to be an array, thus, check if $file is an array before doing an implode.
$file = is_array($file) ? implode("",$file) : $file;
Or even better, use file_get_contents, then you won't need to use implode:
$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);
You should check the $file value for false before joining:
$url = "http://asdsfsfsfsfsdfad.com";
$file = file($url);
if ($file !== false) {
$file = implode("",$file);
if(preg_match("/<title>(.+)<\/title>/i",$file,$m)) {
print "$m[1]";
} else {
print "The page doesn't have a title tag";
}
} else {
print "wrong url";
}
you can check whether $file is array or not ..
if you check it then it will never give you an error..
if(is_array($file) && count($file)>0){
if(preg_match("/<title>(.+)<\/title>/i",$file,$m))
print "$m[1]";
else
print "The page doesn't have a title tag";
}
else{
echo "$file is not arrya so it does not go in the fi block.";
}
You don't need to add the quotes around the file contents string. When you use the function file_get_contents, it already returns the results as a string. By adding those double quotes around it, you are basically adding nothing to the string.
You can use curl to check if the url is valid:
<?
function url_exists($strURL) {
$resURL = curl_init();
curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback');
curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_exec ($resURL);
$intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
curl_close ($resURL);
if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
return false;
}Else{
return true ;
}
}
//Usage Example :
If(url_exists("http://www.weberdev.com/addexample.php3")) {
Echo"URL Exists";
}Else{
Echo"URL doesnot exist";
}
?>
See http://www.weberdev.com/get_example.php3?ExampleID=4335 for more information.

Categories