How to get components from rss using php - php

I want to know to get components from rss using php.
I have created a php code, when I was running it on wampsever it was running well but when I ran it on my website this error was showing
PHP code is
<?php
$feeds = array('http://feeds.feedburner.com/ndtvnews-top-stories?format=xml');
foreach( $feeds as $feed ) {
$xml = simplexml_load_file($feed);
foreach($xml->channel->item as $item)
{
$date_format = "j-n-Y"; // 7-7-2008
$link = $item->link;
echo date($date_format,strtotime($item->pubDate));
echo ''.$item->title.'';
echo '<div>' . $item->description . '</div>';
}
}
?>
and Error is
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]:
http://feeds.feedburner.com/ndtvnews-top-stories?format=xml:1: parser
error : Document is empty in /home/a7989425/public_html/rss.php on
line 5
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]: in
/home/a7989425/public_html/rss.php on line 5
Free Web Hosting
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in
/home/a7989425/public_html/rss.php on line 5
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]:
http://feeds.feedburner.com/ndtvnews-top-stories?format=xml:1: parser
error : Start tag expected, '<' not found in
/home/a7989425/public_html/rss.php on line 5
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]: in
/home/a7989425/public_html/rss.php on line 5
PHP Error Message
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in
/home/a7989425/public_html/rss.php on line 5
PHP Error Message
Warning: Invalid argument supplied for foreach() in
/home/a7989425/public_html/rss.php on line 7
please help
thanks

Related

Curl not working in one server, but OK in all others

below code is not working on one server, but working fine in others. I am getting this error:
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and body in testchecker.php on line 11
Warning: simplexml_load_string(): dden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.20</h3></body> in testchecker.php on line 11
Warning: simplexml_load_string(): ^ in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and html in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Premature end of data in tag body line 1 in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Premature end of data in tag html line 1 in testchecker.php on line 11
Warning: Invalid argument supplied for foreach() in testchecker.php on line 12
<?php
set_time_limit(0);
$url="http://test";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
foreach($xml->User as $child){
echo $child->Id."<br/>";
}
?>
I dont understand why this error is getting, because in local and other servers its working fine. On dreamhost, its not. Can anybody help me in this.
The error message already tells you, what is wrong
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and body in testchecker.php on line 11
Warning: simplexml_load_string(): dden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.20</h3></body> in testchecker.php on line 11
XML requires all tags be closed, while with HTML, you can omit the closing tag occasionally.
In this case, you have a <hr> tag without a corresponding closing tag. This is perfectly valid in HTML. However, it is not well-formed XML, which is required by simplexml_load_string
Description
Takes a well-formed XML string and returns it as an object.
I have fixed the issue.
The ip was blocked, so forbidden message was getting, instead of xml.
Unblocking the ip fixed everything.

PHP Error Reporting to Screen, with Line Breaks

I have setup error reporting in the top of my script to
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); //On or Off
The reporting is being logged/saved to my error log file with line breaks, but displays on the screen as 1 continuous line without breaks.
Screen-output example:
Warning: getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48 Warning: Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49 Warning: imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62 Warning: imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64 Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66 Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72
Error-log file example:
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: getimagesize(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 48
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: Division by zero in /home/xyzxyz/public_html/fetcher.php on line 49
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecreatefromjpeg(i): failed to open stream: No such file or directory in /home/xyzxyz/public_html/fetcher.php on line 62
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecreatetruecolor(): Invalid image dimensions in /home/xyzxyz/public_html/fetcher.php on line 64
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 66
[22-May-2014 16:42:32 Asia/Tokyo] PHP Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/xyzxyz/public_html/fetcher.php on line 72
Is there any way to make the output to screen display with line breaks too, so that it will be easier to read it?
Thank you.
You can use line breaks or HTML in your code to drop in what you need between warnings and errors:
$error = "some warning";
echo $error;
echo $error;
Will output:
some warningsome warning
However, you can add a BR to it to display properly in a browser:
$error = "some warning";
echo $error."<br />";
echo $error."<br />";
Which will output the following in a browser:
some warning
some warning
Alternately, you can use non html line breaks if you are using a terminal, or outputting text to the screen (rather than HTML):
$error = "some warning";
echo $error."\r\n";
echo $error."\r\n";
Which will put in line breaks into normal text.
You can mix and match these together to suit your needs. The other thing you can do is use <pre> and </pre> tags around your error messages so that they display into a browser and/or text file as the error message itself reads:
$error = "some warning";
echo "<pre>";
echo $error;
echo $error;
echo "</pre>";
Edit: If you are generating many warnings/errors that you aren't actually displaying on purpose, you can also modify the following:
html_errors = 1
in your PHP.ini file which will output things into a nice HTML format.
To properly display line breaks in browser, I have the following in my index.php (PHP7.4)
ini_set('error_prepend_string', '<pre>');
ini_set('error_append_string', '</pre>');
ini_set('display_errors', 1);
Furthermore, to wrap the text to screen width, replace the first ini_set with
ini_set('error_prepend_string', '<pre style="white-space: pre-wrap;">');

PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found [duplicate]

I am using PHP for the first time. I am using the php sample for uploading image on ebay sandbox. I am getting the following error on running the PHP file:
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Warning: simplexml_load_string(): HTTP/1.1 200 OK in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Warning: simplexml_load_string(): ^ in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 92
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 93
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 93
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 94
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 94
Relevant lines are:
69. $respXmlObj = simplexml_load_string($respXmlStr); // create SimpleXML object from string for easier parsing
// need SimpleXML library loaded for this
92. $ack = $respXmlObj->Ack;
93. $picNameOut = $respXmlObj->SiteHostedPictureDetails->PictureName;
94. $picURL = $respXmlObj->SiteHostedPictureDetails->FullURL;
What I can understand is the respXMLObj is not getting set properly. I have checked that simleXML support is enabled.
Could someone please help me debug this. Thanks
The code you refer to has this line:
//curl_setopt($connection, CURLOPT_HEADER, 1 ); // Uncomment these for debugging
it seems like you uncommented these. This will result in getting the HTTP header in your response. Which is OK for debugging, but it will create an XML parse error in simplexml_load_string.
Either comment it out again or put 0 as its value.
In my case. I just removed the invisible character The BOM in the beginning of the XML file. How to do it - depends on your text editor.
$hasError = false;
if ( $resp == 'Internal Server Error' || empty($resp) )
{
$hasError = true;
}
if ( ! $hasError )
{
$aux = !empty($resp) ? explode('', $resp) : NULL;
$temp = utf8_decode(trim($aux[0]));
$xml = simplexml_load_string($temp);
}
Do a var_dump($respXmlStr); my guess is that this string is not valid XML.
As per the simplexml-load-string documentation, the first parameter is expected to be A well-formed XML string - http://php.net/manual/en/function.simplexml-load-string.php

Artistdata.com RSS Feed parsing works locally but not on production server

I've been trying for the past couple of days to figure out why my code works perfectly locally but fails when deployed on a production server.
My local testing environment is the latest MAMP on a 10.7.2 Lion iMac.
Basically I need to fetch certain XML RSS data from Artistdata.com in order to insert it into a simple PHP-driven, non-CMS website I'm working on.
<!DOCTYPE html>
<html>
<head>
<title>RSS FEED Parser</title>
</head>
<body>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
# RSS Feed parser #
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
foreach ($x->show as $showEntry) {
echo '<div>';# date
$newDate = new DateTime($showEntry->date);
echo date_format($newDate, 'l, F j, Y');
echo '</div>';# /date
# further data fetching, totally unrelated
# to the problem that I'm experiencing
}
}
?>
<!-- START FEED PARSING -->
<div id="feed-data">
<?php getFeed('http://feeds.artistdata.com/xml.shows/artist/AR-30CA266E4BEDD78F/xml/future'); ?>
</div>
<!-- END FEED PARSING -->
</body>
</html>
I'm sure there are more people who had similar problems but I've yet to find a viable solution.
If you have any pointers I'd be very grateful.
EDIT: Forgot to post the errors, so here they are below
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Space required after the Public Identifier in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : SystemLiteral " or ' expected in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : SYSTEM or PUBLIC, the URI is missing in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 9: parser error : Opening and ending tag mismatch: hr line 7 and body in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: </body></html> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 9: parser error : Opening and ending tag mismatch: body line 4 and html in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: </body></html> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 10: parser error : Premature end of data in tag html line 2 in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/*****/public_html/ssr/parse-feed.php:17 Stack trace: #0 /home/*****/public_html/ssr/parse-feed.php(17): SimpleXMLElement->__construct('<!DOCTYPE HTML ...') #1 /home/*****/public_html/ssr/parse-feed.php(33): getFeed('http://feeds.ar...') #2 {main} thrown in /home/*****/public_html/ssr/parse-feed.php on line 17
Problem solved, I was using the wrong feed, the correct one is http://artistdata.sonicbids.com/john-latini/shows/xml/future
That XML does not look like RSS. It is an specific format defined by http://feeds.artistdata.com/_css/shows.xsd.
The error messages all say that you get an HTML (2.0) page not a XML. I can not reproduce that, I get the XML using file_get_contents().
Try to output the HTML page, maybe it has some more information.
echo file_get_contents('http://feeds.artistdata.com/xml.shows/artist/AR-30CA266E4BEDD78F/xml/future');

How to check that an XML file is good before running simplexml_load_file()

I use PHP's simplexml_load_file() function to call an API that returns changed results based on a Timestamp that I send.
So the API will return only results that have changed since my Timestamp. The problem I am having is if the timestamp is too soon and there are no results for the API to return, then it does not return a proper XML file, instead it will just return a blank page.
This is causing all sorts of problems with simplexml_load_file
Here is a simple test I can run...
$xml = 'http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1';
$xmlObj = new SimpleXMLElement($xml, NULL, TRUE);
This results in...
Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Document is empty in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Start tag expected, '<' not found in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php:9 Stack trace: #0 E:\Server\htdocs\labs\freelance\dogAPI\testorg.php(9): SimpleXMLElement->__construct('http://api.resc...', 0, true) #1 {main} thrown in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Now if I pass the API a Timestamp that is further back where there are results, then it will return a perfect XML document.
I am looking for a way to possibly prevent this nasty error from happening somehow?
simplexml_load_file:
Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.
So suppress the errors and check for FALSE to detect when the query didn't go as expected.
$xml = #simplexml_load_file('http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1');
if(false !== $xml)
{
// Do anything with xml
}
If there an errors # hide it and return false

Categories