When I try to scrape data from some website using say $url using simple html DOM . After few days links get outdated and i get an saying http request failed.So instead of displaying that error.I want to display something else that I want.
For example here is the code :
<?
$html=file_get_html($url);
$title=$html->find("stuff that I want to extract",0)->plaintext;
if($title)
{
echo $title;
}
else{
echo 'problem';
}
?>
In the above example it displays problem only if that data is not found but I need to display problem if I get http request errors too.
error_reporting level is a valid solution. This can throw exceptions and you can use that to handle your errors. There are many reasons why file_get_html might generate warnings, and PHP's manual itself recommends lowering error_reporting.
Or might be this will help you:
$html = file_get_html($url) or die('this is not a valid url');
file_get_html returns false if any issue while getting data.
$html = file_get_html($url);
if ($html) {
// code
} else {
// error
}
Use Try-Catch
try {
$html = file_get_html(trim($url));
} catch (Exception $e) {
// handle error here
}
Suggestion: Use CURL to get the URL and handle the error response.
Related
I am trying to learn how to access soap web services via PHP. I can get a list of functions available. I cannot get a return from a SoapClient function. My code is as follows:
<?php
date_default_timezone_set('America/Chicago');
$fcs = 'fcs is initialized';
$url = 'url is initialized';
$res = 'res is initialized';
$url = 'http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL';
$param = array('ZIP' => '72685');
try {
$client = new SoapClient($url);
$fcs = $client->__getFunctions();
$res = $client->GetCityForecastByZIP($param);
} catch (Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
echo '<br> url = '.$url;
echo '<br> fcs = '.$fcs;
echo '<br> res = '.$res.'<br>';
?>
I have tried about 6 Soap testing URLs that google can find. Some (http://www.webservicex.com/globalweather.asmx?wsdl) of them have evolved into something else. The one I tried the most (http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL) did seem to be having the same trouble I was in it's web page implementation, http://wsf.cdyne.com/WeatherWS/Weather.asmx.
the Exception Error! is
"Server was unable to process request. ---> A network-related or
instance-specific error occurred while establishing a connection to
SQL Server."
To repeat my results,
1) the Soap client object creation seems to work w/o error.
2) __getFunctions seems to work w/o error.
3) trying to get a result from any of the functions produces the error shown above.
Questions:
1) Is there any error in my code that would cause it not to work?
2) What is a good working Soap Web Service URL sandbox?
It looks like its the weather service. To rule out your code go to http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityWeatherByZIP
Add your ZIP into the Zip box and hit 'Invoke' and you will get an SQL error. So I would say its safe to assume its not your code and its the server.
I have this simple code, where I'm trying to access a page with an ID. Whenever I run this code, I get an error "Unsupported get request. Please read the Graph API documentation"
$leheid = $page['accounts'][$x]['id'];
try {
$page = $fb->get("$leheid?fields=events", $at);
$page = $page->getGraphPage();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo $e->getMessage();
}
Okay, but whenever I run the same code without a variable and with a string, no errors...
If I echo or print the same variable I get the right string with no spaces or anything else in there, so I'm quite confused...
Also, I tried $leheid. '....'
It's an old question but i would like to contribute and maybe help someone else with the same problem.
The reason why the code above didn't work, was because i mistyped the API response function
In short:
$page = $page->getPage();
should have been replaced with
$page = $page->getGraphPage()->asArray();
Which will also give you the response as an array
Im creating a web service (my server is ubuntu vm, not using WAMP or XAMPP) but having an issue = Exception:looks like we got no XML document. I have searched the internet but there are mostly soap exceptions for this, but I am not having a soap fault. It is a plain exception. It is echoing hi, but not echoing hi4, so I guess the problem might be in the function viewHealthDetails (which is in my wsdl) but I am almost sure that my wsdl is correct as I have used it previously in another project. Please help.
if(isset($_POST['txtInput']))
{
try
{
$input=$_POST['txtInput'];
$wsdl='.../Search.wsdl';
$options=array('cache_wsdl'=>WSDL_CACHE_NONE,'features'=>SOAP_SINGLE_ELEMENT_ARRAYS);
$client=new SoapClient($wsdl,$options);
echo "hi";
$response=$client->viewHealthDetails($input);
echo "hi4";
if(isset($response->HealthDetails))
{
$HTMLDocument="<!Doctype html>
// My html code
}
else
{
echo "<h1>This Health type is not in our categories!</h1>";
}
}
catch(Exception $e)
{
echo 'Exception:'.$e->getmessage();
}
catch (SOAPFAULT $exception)
{
echo 'SOAP Exception: '.$exception->getMessage();
}
}
else
{
}
This error is being thrown because the PHP cannot process the response XML correctly. To troubleshoot the response, set your options array to:
$options=array('cache_wsdl'=>WSDL_CACHE_NONE,'features'=>SOAP_SINGLE_ELEMENT_ARRAYS, 'trace' => 1);
then use var_dump($client->__getLastResponse()); after making the request. This will allow you to inspect the returned XML and validate it for correctness. You may find there is other output that is breaking the XML.
I'm using PEAR XML_Feed_Parser.
I have some bad xml that I give to it and get error.
DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE8 0xCF 0xD3 0xD4 in Entity, line: 7
It's actually html in wrong encoding - KOI8-R.
It's ok to get error but I can't handle it!
When I create new XML_Feed_Parser instance with
$feed = new XML_Feed_Parser($xml);
it calls to __construct() that looks like that
$this->model = new DOMDocument;
if (! $this->model->loadXML($feed)) {
if (extension_loaded('tidy') && $tidy) {
/* tidy stuff */
}
} else {
throw new Exception('Invalid input: this is not valid XML');
}
Where we can see that if loadXML() failed then it throw exception.
I want to catch error from loadXML() to skip bad XMLs and notify user. So i wrapped my code with try-catch like that
try
{
$feed = new XML_Feed_Parser($xml);
/* ... */
}
catch(Exception $e)
{
echo 'Feed invalid: '.$e->getMessage();
return False;
}
But even after that I get that error
DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE8 0xCF 0xD3 0xD4 in Entity, line: 7
I've read about loadXML() and found that
If an empty string is passed as the source, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.
But somehow instead of warning i get error that halts my application. I've written my error handler and I saw that this is really warning ($errno is 2).
So i see 2 solutions:
Revert warnings to warnings - do not
treat them like errors. (Google
doesn't help me here). After that
handle False returned from loadXML.
Somehow catch that error.
Any help?
libxml_use_internal_errors(true) solved my problem. It made libxml to use normal errors so i can catch False from loadXML().
Try this one:
$this->model = new DOMDocument;
$converted = mb_convert_encoding($feed, 'UTF-8', 'KOI8-R');
if (! $this->model->loadXML($converted)) {
if (extension_loaded('tidy') && $tidy) {
/* tidy stuff */
}
} else {
throw new Exception('Invalid input: this is not valid XML');
}
or you can do it without need to modify XML_Feed_Parser like this:
$xml = mb_convert_encoding($loaded_xml, 'UTF-8', 'KOI8-R');
$feed = new XML_Feed_Parser($xml);
Which would you recommend?
Return an error code, such as E_USER_ERROR from a function, and determine proper message higher up:
function currentScriptFilename()
{
if(!isset($_SERVER['SCRIPT_FILENAME']))
{
//This?
return E_USER_ERROR;
}
else
{
$url = $_SERVER['SCRIPT_FILENAME'];
$exploded = explode('/', $url);
return end($exploded);
}
}
Execute trigger_error() from the function, with a specific error message:
function currentScriptFilename()
{
if(!isset($_SERVER['SCRIPT_FILENAME']))
{
//Or this?
trigger_error('$_SERVER[\'SCRIPT_FILENAME\'] is not set.', E_USER_ERROR);
}
else
{
$url = $_SERVER['SCRIPT_FILENAME'];
$exploded = explode('/', $url);
return end($exploded);
}
}
I am not sure if I will regret having put a bunch of error messages in my functions further down the line, since I would like to use them for other projects.
Or, would you recommend something totally different?
Do not mix the matters.
Error notification and error handling are different tasks.
You have to use both methods simultaneously.
If you think that $_SERVER['SCRIPT_FILENAME'] availability is worth an error message, you can use trigger error. However PHP itself will throw a notice if you won't check it.
If you want to handle this error, just check this function's return value.
But I would not create a special function for this task.
So,
if (!$filename = basename($_SERVER['SCRIPT_FILENAME']) {
// do whatever you want to handle this error.
}
would be enough
Exceptions could be useful to handle errors, to know if we had any errors occurred.
A simple example:
try {
$filename = basename($_SERVER['SCRIPT_FILENAME'])
if (!$filename) throw new Exception("no filename");
$data = get_some_data_from_db() or throw new Exception("no data");
$template = new Template();
//Exception could be thrown inside of Template class as well.
}
catch (Exception $e) {
//if we had any errors
show_error_page();
}
$template->show();
3.Use exceptions.
If this is the route you are going, I'd rather recommend throwing Exceptions rather then returing an E_ERROR (E_USER_ERROR should be used), as this is just an integer, and possibly a totally valid return for your function.
Advantages:
- Throwing of an Exception cannot be interpreted as anything else then an error by mistake.
- You keep the possibility to add a descriptive error message, even though you don't handle the error at that point/
- You keep a backtrace in your Exception.
- You can catch specific exceptions at specific points, making the decision where in your project a specific type of error should be handled a lot easier.
If not using exceptions which you should be, use trigger_error().
If it is an error you'd like to deal with, try returning FALSE like a lot of the in built functions do.
If you do use exceptions, catch them like this
try {
whatever()
} catch (Exception $e) {
// handle however
}