Bypassing PHP XML errors when offline - php

I have local php website that has some online and some offline components. It pulls xml data from various websites normally. When offline, the page will not render due to various errors.
I use $xml = simplexml_load_file($url); to retrieve xml data. When offline I usually get errors telling me that the target url doesn't exist and that certain nodes in the xml file cannot be retrieved.
How do I bypass these errors so that the rest of the page can be loaded when offline?

I solved the issue.
$var = #simplexml_load_file($url);
if(!$var){
echo "error";
}
else{
execute code...
}
Now when I'm offline, the script will leave an error message if it cannot connect and continue processing the page. Before, it would reach an error and stop loading the rest of the page.

Related

mssql_execute causing Error code: ERR_EMPTY_RESPONSE

I am having an issue with a site that I am moving onto a new server. This site is connected to a SQL Server database, and is working correctly in it's current location. However, the new location is having problems with a single mssql_execute. Here is a sample of the code:
function get_customer_select_info() {
$link = get_db_link();
$customer_all_command_text = "dbo.Customer_All";
$customer_all_stmt = mssql_init($customer_all_command_text, $link);
$search = "%";
mssql_bind($customer_all_stmt, "#parm1", $search, SQLVARCHAR, false, false, 15);
$result = mssql_execute($customer_all_stmt);
mssql_free_statement($customer_all_stmt);
return $result;
}
Calling the above function results in
Unable to load the webpage because the server sent no data.
Error code: ERR_EMPTY_RESPONSE
from Chrome.
get_db_link retrieves a new connection to the database, or returns the current one if it already exists. It is functioning correctly. I have tried just closing the link and creating a new one if one already exists, which had no effect on the error.
This is not a problem connecting to the database because before this stored procedure is executed, three others are called and executed correctly.
Another complication is that running a trace on the given database results in this stored procedure getting called on the database and being successful!
Commenting out the mssql_execute makes the page load correctly (minus the information that this function returns).
Again, the same exact code on the current server works correctly, but the code on the new server is causing problems. I can't obtain any debugging information from apache or php because it is not actually sending anything.
If you facing ERR_EMPTY_RESPONSE then i will suggest you to follow these steps
Refresh page and restart browser
Clean your cache and browser cookies
Check internet connection
Update Google Chrome
Check router
I am also sharing video tutorial where you will find the complete solution of given steps to resolve your problem here is the link
http://vimeo.com/60824619
Best of luck

PhP reading script erros from console

i am trying to test if my script is loaded on a given website and if the script is actually working without any errors onload (later on i will have to do the same for onclick)
So far i have
$testResult = array();
$homepage = 'http://www.example.dk/';
$data = file_get_contents($homepage);
if (strpos($data,'example_script.js'))
{
$testResult['scriptLoaded'] = true;
print_r("win");
}else{
$testResult['scriptLoaded'] = false;
}
Now this loads the page and checks if the javascript is on the page. But how can i read from the console to check if there is any errors while loading the script?
Also is this the right way to check if the script is on the page? The only restriction i have is that i HAVE to use PHP.
The only thing you can check with your code is weather or not somewhere in the code/contents you've gotten from the given url, there is a string example_script.js. If you were to use the url to this page, you'd get true and "win", too, because the substring will be found.
The JS might be riddled with fauklts, but since PHP doesn't understand Js, you won't be able to see that.
If you want to test your site, without a browser, the only thing I can think of is using phantomjs:
Which can be found Here
Using PHP alone, you might be able to do a couple of checks using scriptable browser, cUrl, and the DOMDocument class (to parse and validate the markup).

How to check if the web connection is established?

Disambiguation: 'connection' doesn't mean database connection.
REAL QUESTION
Scenario 1: I am taking weather feed from a 3rd party site, and it's working fine. Suddenly my client complained that it's broken, and showing an error. Then quickly I just simply commented out the codes.
Scenario 2: Suppose I embedded Google fonts into my website and it's looking fine. Suddenly Google's services are banned in a country, and my site looks dumb.
PROBABLE SOLUTION
If I can put a checker like:
<?php
// suppose this is the URL of my connection I need to connect my site on loading
$connection = http://www.feed-from-somewhere.com/feed/my-feed123
if ( isset( $connection ) ) {
// show the feed from 3rd party site
} else {
// do my backup plan for the feed instead
}
?>
But I can't find any way. If I can do such thing than it would be better for all such 3rd party sharing and site won't malfunction in future.
Waiting for a nice solution...
Maybe you can try the fopen() function in php.
as described in the php.net fopen() --
"If filename is of the form "scheme://...", it is assumed to be a URL
and PHP will search for a protocol handler (also known as a wrapper)
for that scheme."...
example could be:
#$connection = fopen("http://iewuhf.com/", "r");
//note # is used to suppress the error ortherwise, if connection fails, a warning
//will be displayed
if(!$connection){
echo 'false';
}else{
echo 'true';
}

Load/Modify/Save XML with javascript

Before this question gets stamped as a duplicate, I am sorry! I've read ALL the duplicate questions and if anything, it has confused me even more. So maybe this question is slightly different.
I've written a little Javascript library that makes ajax calls and fetches and parses information from the graph facebook API.
This enables me to pretty much show all my page status' on my web page. However I'm just about to launch, and I have done as much testing as I can.
However. I'm sure errors will occur, and I've written many error catches blah blah blah.
What I want to do, is save all my errors in a xml file.
So when an error occurs, I want the javascript to load the xml file from the server, add the errors, then save the changes.
I know how to load the xml doc using XmlHttpRequests, And I'm sure I can figure out how to modify the xml just by using dom manipulation.
All i really want to know is. How do i save these changes? does it save automatically?
Or do i have to "somehow" pass the updated xml version to php and get that to save it?
Im not quite sure how to go about it.
I would use mySQL and php but that means "somehow" passing the error information to php, then saving it.
However id much prefer XML seeing as I'm the only person that will be reading the xml file.
Thanks very much.
Alex
Or do i have to "somehow" pass the updated xml version to php and get that to save it?
Yes, you'll want to use an XML HTTP request to send the XML DOM to the server where PHP can save it:
function postXML(xmlDOM, postURL, fileName, folderPath){
try{
// Create XML HTTP Request Object
oXMLReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
// Issue Synchronous HTTP POST
oXMLReq.open("POST",postURL,false);
// Set HTTP Request Headers
if(fileName != null){ oXMLReq.setRequestHeader("uploadFileName", fileName); } // What should file be named when saved on server?
if(folderPath != null){ oXMLReq.setRequestHeader("uploadDir", folderPath); } // What folder should file be saved in on server?
// SEND XML
///WScript.Echo(xmlDOM.xml);
oXMLReq.send(xmlDOM.xml);
return oXMLReq.responseText;
}catch(e){
return "postXML failed - check network connection to server";
}
}

PHP CURL - how to tell if the entire file requested was not fully downloaded

I'm using CURL and a proxy to grab some xml files, occasionally only part of the XML document comes through and fails when I try to load/use the xml (simplexml_load_string).
I thought something like..
if(curl_errno($ch))
{
$error = curl_error($ch);
// handle error
}
would catch this sorta error via CURL errno..
CURLE_PARTIAL_FILE (18)
A file transfer was shorter or larger
than expected. This happens when the
server first reports an expected
transfer size, and then delivers data
that doesn't match the previously
given size.
However, this does not work, I think it might be due to using a proxy. Anything else I can check? My only thought now is to do a preg_match for the last bit of the XML document, but that seems less than ideal as I'm getting multiple types of XML documents and I'd have to write checks for each type.
I have experienced the same problem with a proxy and I fell short of solving the issue using cURL's error handlers. If you have access to both scripts (the one requesting and the one delivering the XML) have the requesting one deliver a unique code which it expects at the end of the XML:
// Request
http://localhost/getxml.php?id=4&uniq=1337
And append a comment at the end of the output:
<?xml encoding="..." ..?>
...
<!--1337-->
Well, having an error already tells you that the XML file you got is invalid. What you need to do is catch that error and handle it.
One quick fix is this:
$xml = #simplexml_load_string($xmlString);
if($xml === false){ /* The XML was not valid. */ }
One log fix is this one:
libxml_use_internal_errors(true);
libxml_clear_errors();
$xml = simplexml_load_string($xmlString);
if( ($err = libxml_get_last_error()) !== false ){ /* We got ourselves an XML error. */ }

Categories