php: simplexml_load_file check server status - php

i load file from server:
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
And if servers is close i get error:
Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity
how to check if the file is reached?

From the manual page for simplexml_load_file
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.
which means you can do
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
// check what was returned
if(FALSE === $xml) {
echo 'could not open file';
}

Related

Need to parse a photo with DuckDuckGo

<?php
$get = file_get_contents("https://duckduckgo.com/i.js?q=love&s=1");
$decode = json_decode($get, TRUE);
foreach($decode['results'] as $res) {
echo "".$res['image']."";
}
?>
Nothing happens. What's wrong?
Warning: file_get_contents(https://duckduckgo.com/i.js?q=love&s=1) [function.file-get-contents]: failed to open stream: No such file or directory in /home01/rfep/nzfrka/home/mydomain/myfeed/sssfi.php on line 2
failed to open stream
Warning: Invalid argument supplied for foreach() in /home01/rfep/nzfrka/home/mydomain/myfeed/sssfi.php on line 4

simple_html_dom.php failed to open stream: No such file or directory

Im trying to use the simple html dom parser within WAMP - for some reason I can't get the php file to recognise the parser - I'm using example code from the parser website and it is not working - the code is as follows;
<?php
include_once('C:\wamp\www\reports\simple_html_dom.php');
function scraping_digg() {
// create HTML DOM
$html = file_get_html('http://digg.com/');
// get news block
foreach($html->find('div.news-summary') as $article) {
// get title
$item['title'] = trim($article->find('h3', 0)->plaintext);
// get details
$item['details'] = trim($article->find('p', 0)->plaintext);
// get intro
$item['diggs'] = trim($article->find('li a strong', 0)->plaintext);
$ret[] = $item;
}
// clean up memory
$html->clear();
unset($html);
return $ret;
}
// -----------------------------------------------------------------------------
// test it!
// "http://digg.com" will check user_agent header...
ini_set('user_agent', 'My-Application/2.5');
$ret = scraping_digg();
foreach($ret as $v) {
echo $v['title'].'<br>';
echo '<ul>';
echo '<li>'.$v['details'].'</li>';
echo '<li>Diggs: '.$v['diggs'].'</li>';
echo '</ul>';
}
?>
So far I have tried having the path as follows;
include_once('C:\wamp\www\reports\simple_html_dom.php');
include_once('http://localhost/reports/simple_html_dom.php');
include_once('simple_html_dom.php');
Here are the error messages I recieve
) Warning: include_once(../../simple_html_dom.php): failed to open stream: No such file or directory in C:\wamp\www\reports\example_scraping_digg.php on line 2
Warning: include_once(): Failed opening '../../simple_html_dom.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\reports\example_scraping_digg.php on line 2
I also get another error on line 6..;
Fatal error: Call to undefined function file_get_html() in C:\wamp\www\reports\example_scraping_digg.php on line 6
Thanks in advance for any help you can offer - It is greatly appreciated.
The file in question is located here : C:\wamp\www\reports\simple_html_dom.php
which is why I'm so confused - Thanks again for your help
After hours of looking at the problem - It was incorrect php.ini settings in the WAMP folder - Thanks

Preventing php_network_getaddresses: getaddrinfo failed:

I am trying to call an URL with PHP to check if it exists and is reachable.
My initial code was
fopen('http://'.$this -> url, 'r');
But it throws the following errors every time an URL is unreachable:
fopen(http://dwzegdzgwedzgew.com): failed to open stream: php_network_getaddresses: getaddrinfo failed
php_network_getaddresses: getaddrinfo failed:
The error operator (#) is ignored in this case as the error isn't thrown by fopen itself but while resolving the asdress. So I thought that should do it:
#fopen(#'http://'.$this -> url, 'r');
but it also goes on throwing the error.
Is there a non-error-throwing possibility to check if an URL exists within PHP before opening it?
What is the error message with $ressource = #fopen('http://' . $this->url, 'r'); ?
<?php
$urls = array('kihgkighuhgkig.li', 'google.com', 'adsfafdf.fr');
foreach ($urls as $url)
{
if (gethostbyname($url) != $url)
{
$ressource = fopen('http://' . $url, 'r');
var_dump($url, $ressource);
}
}
The output is :
$> php test.php
string(10) "google.com"
resource(6) of type (stream)

simplexml_load_file fails with '&' in xml feed

Error:
Warning: simplexml_load_file(): feed.rss:2393: parser error : xmlParseEntityRef: no name in C:\xampp\htdocs\mklbet\index.php on line 16
Warning: simplexml_load_file(): <title>We have a winner & it is ME</title> in C:\xampp\htdocs\mklbet\index.php on line 16
My problem is that when i use simplexml_load_file($url) - and the in the xml feed there is a '&' it will make an error - how can i get this to work?
if (file_exists($url)) {
$tips = simplexml_load_file($url);
} else {
exit('Failed to open stream');
}
foreach ($tips->channel->item as $entry) {
$title=htmlentities($entry->title);
}
}
This is how i wrote the code, i am getting the rss feed from another site so i cant edit the character

Checking whether xml files are empty or not

If i push a string which doesn't exist or misspelled to function benzer($ilk_artist) i am getting errors bottom. Whether i push a valid artist name or not variable $completeurl is always defined. So i can't put if($completeurl) I think i should control whether $completeurl is valid before simplexml_load_file($completeurl). Do you have an idea that how i can do it?
function benzer($ilk_artist)
{
$completeurl = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist='.trim($ilk_artist).'&api_key='.LASTFM_APP_ID;
$completeurl = urlencode($completeurl);
$xml = simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}
$artists = $xml->similarartists->artist;
$length = count($artists);
for ($i = 0; $i < $length; $i++) {
$artistname[$i] = $artists[$i]->name;
}
return simplexml_kurtul($artistname);
}
errors:
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file(http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=tgyu+6uh6n&api_key=APIKEY) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
in /home6/.../public_html/.../functions/fns.php on line 102
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity "http%3A%2F%2Fws.audioscrobbler.com%2F2.0%2F%3Fmethod%3Dartist.getsimilar%26artist%3Dtgyu+6uh6n%26api_key=APIKEY0" in /home6/.../public_html/.../functions/fns.php on line 102
What about not printing out the warnings by adding '#'?
$xml = #simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}

Categories