Simple HTML Dom error when site doesn't exist - php

My script try to open every site from a file read line by line but when try to parse a site which doesn't exist the script stop and give me these error:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76
Warning: file_get_contents(http://www.thissitedoesntexist.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\test2.php on line 11
How can I fix it ? How can I make it to run even if a site doesn't exist... to read a new line from my file(which means to read another site and then to read it) Also the scrip stop when receive errors like 404, 403, etc.

I would do this, to check for the error codes ---
foreach ($sites as $site) {
$ch = curl_init('http://'.$site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
curl_exec($ch);
// Check if any error occurred
$info = curl_getinfo($ch);
if($info['http_code'] != 200) {
continue;
}
//rest of loop, here --
}
You could even do something different depending on the error code you get with a case-switch --

According to the documentation, file_get_contents() returns FALSE on failure.
So, check what it returns to be sure that the site exists before trying to parse the returned content. If it doesn't exist, iterate to the next line in the file and keep continue the process:
if($file = file_get_contents("http://www.thissitedoesntexist.com")) {
// Parse file here
// Then continue reading the file by
// starting at the next line.
continue;
}
Reference:
file_get_contents()

Related

PHP Fatal error: Call to undefined method JDocumentError::close()

I've searched for a solution everywhere but I couldn't find an answer. Some days I got this error without change or do anything:
And I can't access my website in the entire day but suddenly starts working again without any change, and some days later the same problem. What can I do?
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator at
webmaster#magic.kiwitextil.com to inform them of the time this error
occurred, and the actions you performed just before this error.
More information about this error may be available in the server error
log.
Additionally, a 500 Internal Server Error error was encountered while
trying to use an ErrorDocument to handle the request.
My site is: www.magic.cr
I can access to the backend always! (magic.cr/administrator)
I've checked my server logs and I got this lines repeating constantly:
[23-Aug-2016 16:44:53 UTC] PHP Deprecated: Non-static method
JApplication::redirect() should not be called statically, assuming
$this from incompatible context in
/home/kiwitex/public_html/magic.cr/templates/braink/error.php on line
9
[23-Aug-2016 16:44:53 UTC] PHP Notice: Undefined property:
JDocumentError::$_messageQueue in
/home/kiwitex/public_html/magic.cr/libraries/legacy/application/application.php
on line 376
[23-Aug-2016 16:44:53 UTC] PHP Notice: Undefined property:
JDocumentError::$client in
/home/kiwitex/public_html/magic.cr/libraries/legacy/application/application.php
on line 394
[23-Aug-2016 16:44:53 UTC] PHP Notice: Trying to get
property of non-object in
/home/kiwitex/public_html/magic.cr/libraries/legacy/application/application.php
on line 394
[23-Aug-2016 16:44:53 UTC] PHP Fatal error: Call to
undefined method JDocumentError::close() in
/home/kiwitex/public_html/magic.cr/libraries/legacy/application/application.php
on line 409
/home/kiwitex/public_html/magic.cr/libraries/legacy/application/application.php
Lines 376 to 409
if (count($this->_messageQueue))
{
$session = JFactory::getSession();
$session->set('application.queue', $this->_messageQueue);
}
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . str_replace("'", "&apos;", $url) . "';</script>\n";
}
else
{
$document = JFactory::getDocument();
jimport('phputf8.utils.ascii');
if (($this->client->engine == JApplicationWebClient::TRIDENT) && !utf8_is_ascii($url))
{
// MSIE type browser and/or server cause issues when url contains utf8 character,so use a javascript redirect method
echo '<html><head><meta http-equiv="content-type" content="text/html; charset=' . $document->getCharset() . '" />'
. '<script>document.location.href=\'' . str_replace("'", "&apos;", $url) . '\';</script></head></html>';
}
else
{
// All other browsers, use the more efficient HTTP header method
header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
header('Location: ' . $url);
header('Content-Type: text/html; charset=' . $document->getCharset());
}
}
$this->close();
/home/kiwitex/public_html/magic.cr/templates/braink/error.php
Lines 8 to 10
case "404":
JApplication::redirect("index.php/404-error"); // replace the redirect URL with your own
break;
Please help I cannot access my site in the last 36 hours!!!

Error handling file get contents

I am trying to create a simple script with file_get_contents and Facebook graph. However, there seems to be some issue with the error handling.
Here is the code I have:
$json = file_get_contents('https://graph.facebook.com/', true);
if($json !== FALSE) {
echo "test";
}
It returns the following error message:
Warning: file_get_contents(https://graph.facebook.com/): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/test/tst/mellemmand/test.php on line 4

PHP failed to open stream: HTTP request failed! 503 error

I am using PHP Simple HTML DOM Parser to get a data from a website. I call this following function in a while loop say for example 15 actors data to be scraped. what happens is after scraping data for 5 or 6 or somewhere in loop, it fails to open the stream and breaks the script. I used try and catch function. but still i am not to fix it out. Actually my need is even if it fails in the middle of a loop, it has to go on till the end. Can anyone assit me pls.
function get_data($actor) {
try {
$html = file_get_html('http://someurl.com', false, $context);
if (isset($html)) {
$table1 =$html->find('table.table_border',0) ;
// doing some process here
}
} catch(Exception $e) {
echo $tt_id."-Failed once.<br>";
}
}
Sometimes i am getting the following error and the script stops.
Warning: file_get_contents(http://someurl.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable
Fatal error: Call to a member function find() on a non-object in somefile.php on line 37

Connecting to api through php (traileraddict)

I'm connecting to traileraddict.com. I can make the connection and display the 8 newest trailers in localhost. But when I load the page to the internet it won't display.
here is the exsample from trailer addict
<?php
$upcoming = simplexml_load_file("http://api.traileraddict.com/?featured=yes&count=8");
foreach($upcoming->trailer as $x => $updates)
{
echo $updates->title;
echo '<br>';
echo '<span style="font-size:x-small">Source: TrailerAddict</span>';
echo '<br>';
//now echo the embedded trailer
echo $updates->embed;
echo '<br><br>';
}
?>
And here is the error message I receive when I load it into the server.
Warning: simplexml_load_file(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Warning: simplexml_load_file(http://api.traileraddict.com/?featured=yes&count=8): failed to open stream: no suitable wrapper could be found in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://api.traileraddict.com/?featured=yes&count=8" in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Notice: Trying to get property of non-object in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 5 Warning: Invalid argument supplied for foreach() in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 5
Bare in mind that don't have access to the server witch host's the site and I have also checked out related subject on stack, witch I don't under stand at all. So if you could help me that would be fantastic
That is because the server's configuration prevents it from directly opening URLs from simplexml_load_file(), so you should first download the page using Curl :
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://api.traileraddict.com/?featured=yes&count=8");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// load the previously downloaded XML page
$upcoming = simplexml_load_string($output);
// continue as usual (foreach $upcoming...)

PHP fatal error: Uncaught exception 'Exception' with message

I have a PHP script that connects to an api and posts information to their systems, but when its trying to connect it throws a fatal error:
Fatal error: Uncaught exception 'Exception' with message 'Problem with
'http://apitestserver.co.uk:9000/Service.svc/Items' in
/var/www/html/e/connect_test.php:17 Stack trace: #0
/var/www/html/e/connect_test.php(39):
do_http_request('http://apitest....', 'hello') #1 {main} thrown in
/var/www/html/e/connect_test.php on line 17
If I send it to a PHP script which just grabs the IP then it works, but if I send it to the API it doesn't. My PHP script creates XML and then forwards to the server. I was getting errors so I just created the following smaller script purely to test the connection:
function do_http_request($url, $data, $method = 'POST',
$optional_headers = 'Content-Type: application/atom+xml') {
$params = array(
'http' => array(
'method' => $method,
'content' => $data
)
);
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
$metaData = stream_get_meta_data($fp);
fclose($fp);
if(!preg_match('~^HTTP.*([0-9]{3})~',
$metaData['wrapper_data'][0], $matches)){
throw new Exception('MALFORED RESPONSE - COULD NOT UNDERSTAND HTTP CODE');
}
if (substr($matches[1], 0, 1) != '2') {
throw new Exception('SERVER REPORTED HTTP ERROR ' . $matches[1]);
}
return $response;
}
$data = 'hello';
$paul =
do_http_request('http://apitestserver.co.uk:9000/Service.svc/Items',$data);
echo $paul;
If I change the URL to a simple script on another one of our servers which just grabs the IP of the incoming connection and returns it:
$ip=$_SERVER['REMOTE_ADDR'];
echo 'IP equals = ' . $ip;
Then it works fine with no errors.
Update -
with errors on it throws the following warning, probably because the script is not sending the correct info to the API
Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items)
[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1
500 Data at the root level is invalid. Line 1, position 1.
Also note that I can access the api server fine using fiddler to send manually created items across, its just when this script tries to connect and send data there is an issue. I wrote another quick script which connects and prints out the default response (an rss feed of submitted items)
When I run the 'main' connector script it throws the following two errors
Warning: fopen() [function.fopen]: php_network_getaddresses:
getaddrinfo failed: Name or service not known in
/var/www/html/e/sequence.php on line 65
Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items)
[function.fopen]: failed to open stream: Operation now in progress
in /var/www/html/e/sequence.php on line 65
I would suggest using curl instead of fopen(). curl is both more flexible, and more secure. Many servers disable allow_url_fopen, curl also fails more gracefully when there are problems on the remote server.
I just tested the URL in my browser and got a connection error.
Maybe the problem is with their server (or you have the wrong URL)?
Edit:
Looks like ther server is throwing a 500 error - maybe because the data you're posting is invalid.
You could use a packet sniffer and check the exact data you're sending to the server (or use cURL as suggested by #acrosman)
apitestserver.co.uk doesn't respond to ping and the url is inaccessable. The API-server doesn't seem to be up.
or You can change the headers:
$optional_headers = 'Content-Type: application/json')
First error message suggests problems with apitestserver - it returns error code 500, which states for "Internal server error".
Second one informs that PHP can't resolve host name into IP address - I'd look into DNS configuration and/or hosts file.
Apparently 'fopen' function does not return proper stream. There can be many possibilities when that happens, but according to PHP manual, fopen function on fail should present E_WARNING message with proper commentary. It is possible that you have errors of that class silenced - call error_reporting(E_ALL) at the beginning of your script and look for some relevant error messages.

Categories