simplexml_load_file fails with '&' in xml feed - php

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

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

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;
}

YouTube script that returns playlist thumbnail giving errors?

I have had lots of help with creating a PHP script that basically grabs the latest titles and thumbnails of videos from a YouTube playlist. This used to work, but somehow it has stopped working. Could anybody help?
The php script is running at http://new.fearofmobs.com/playlist.php, i've switched on error reporting. The script is mean't to return thumbnails from a YouTube playlist and cache them hourly. The code
<?php error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors', 1);?>
<?php
$cachefile = 'videobrowser.txt';
$cache_timer = 3600 + #filemtime($cachefile);// files timestamp + 3600 seconds
if (file_exists($cachefile) && time() < $cache_timer ) {
}
else {
$data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2?orderby=published&max-results=12");
$fh = fopen($cachefile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
$thumbnail ='';
$data = simplexml_load_file($cachefile);
$xml = simplexml_load_string($data);
foreach($xml->entry as $playlist){
$media = $playlist->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->thumbnail[1]->attributes();
$thumb = $attrs['url'];
$attrs = $media->group->player->attributes();
$video = $attrs['url'];
$title = substr( $media->group->title, 21);
$url = $video;
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
$vid_Id = $my_array_of_vars['v'];
###################NEW CODE
$toggle = 'hotlink';// replace 'hotlink' with 'null' to save images locally
if ($toggle == 'hotlink'){
$image_ID = $thumb; // hotlink images from YouTube
}
else{
///////////////////////////////// Save Images To Local Webserver
///////////////////////////////// just in case Youtube objects to hotlinking
$image_ID = $vid_Id.".jpg"; /// or use sub folder for neatness -> "images_folder/".$vid_Id.".jpg"
$image_saved = #filemtime($image_ID);// # is used to suppress the error caused by the image not having been seen before
if (!$image_saved){/// if you can't find it on local server go fetch it and save to the sites server
file_put_contents($image_ID, file_get_contents($thumb));
//// you can delete the line below
echo ' fecthed image >> '.$image_ID."<br>" ;
//// you can delete the line above
}//// close if image saved
} ##################### END NEW CODE
$thumbnail .= '<div style="float:left; cursor:pointer;">
<p class="crop"><a class="videobox various iframe" href="http://www.youtube.com/embed/' . $vid_Id . '?autoplay=1&hd=1" onclick=swapper('. $vid_Id .')><img src="' .$image_ID . '" title="' . $title . '" width="74" height="56"/></a></p></div>';
}
?>
<?php echo $thumbnail; ?>
The errors
Warning: simplexml_load_file(): videobrowser.txt:1: parser error : Document is empty in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): ^ in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): videobrowser.txt:1: parser error : Start tag expected, '<' not found in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): ^ in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: Invalid argument supplied for foreach() in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 18
If anybody needs any further information please do let me know.
As far as I can tell, you are not entering the else clause that populated videobrowser.txt:
if (file_exists($cachefile) && time() < $cache_timer ) {
// you are going here
}
else {
// but you should be going here
$data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2?orderby=published&max-results=12");
$fh = fopen($cachefile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
I'm guessing that at some point videobrowser.txt was created with no content (a completely empty file). This means that you are not re-populating the file, and are expecting it to be filled.
Check and see if videobrowser.txt exists, and if it does, delete it.

php: simplexml_load_file check server status

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';
}

Categories