simplexml


simplexml_load_file error in PHP 5.3


I'm using the following code to read an RSS feed and output the results.
function home_page_parser($feedURL) {
$rss = simplexml_load_file($feedURL);
$i = 0;
echo "<ul>";
foreach ($rss->channel->item as $feedItem) {
$i++;
$myDate = ($feedItem->pubDate);
$dateForm = explode(" ", $myDate);
echo "<li class=\"rss-feed\">".$feedItem->title."<br />" .$feedItem->pubDate. "</li>";
if($i >= 3) break;
echo "</ul>";
}
}
It is working fine on my testing site at Rackspace Cloud running PHP 5.2
On the live site at Media Temple running PHP 5.3, I get the following errors:
Warning: simplexml_load_file() [function.simplexml-load-file]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /.../html/includes/functions.php on line 39
Warning: simplexml_load_file(http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /.../html/includes/functions.php on line 39
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml" in /.../html/includes/functions.php on line 39
Warning: Invalid argument supplied for foreach() in /.../html/includes/functions.php on line 44
Line 39 is this:
$rss = simplexml_load_file($feedURL);
What am I doing wrong or needs to change to work on 5.3?
The error is pretty descriptive dont you think?
http:// wrapper is disabled in the server configuration by
allow_url_fopen=0
You will need to edit the PHP configuration file and change the configuration allow_url_fopen. If you cant do this directly try ini_set()
Edit: As #evanmcd pointed out in the comments, this configuration can only be set in php.ini. PHP documentation reference.
This error comes due to "http:// wrapper is disabled in the server configuration by allow_url_fopen=0" .For avoiding this issue we need to override this setting to On instead off.In my view most of shared hosting servers do not allow you to do these setting through either ini_set('allow_url_fopen', 'on'); or htaccess overriding.So instead of trying these methods I suggest a way to fetch that feed is as follows.Using CURL we need to fetch the content of feed xml to a variable.Then process our simplexml file operations .
Example
$feed ='http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=mytwittername';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get the result of http query
$output = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_file($output);
ini_set("allow_url_fopen", 1);
This will set allow url open = On in php.ini file but you need to restart php in easyphp or xamp or wamp or in hosting.
If you are not allowed to edit php.ini in server you can use curl to get xml and read xml stirng as below.
function home_page_parser($feedURL) {
$rss = simplexml_load_file(curlXML($feedURL);
$i = 0;
echo "<ul>";
foreach ($rss->channel->item as $feedItem) {
$i++;
$myDate = ($feedItem->pubDate);
$dateForm = explode(" ", $myDate);
echo "<li class=\"rss-feed\">".$feedItem->title."<br />" .$feedItem->pubDate. "</li>";
if($i >= 3) break;
echo "</ul>";
}
}
function curlXML($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get the result of http query
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

Related Links

SimpleXML Sigle Depth Element with Attribute in Inline List
Copying CDATA between 2 xml files
PHP 7 simpleXML fatal error
SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found
Add xml attribute documentname
Pick shortest route with simpleXML
SimpleXML extract text between two tags
XML Node outside parent item
SimpleXMLElement cannot get the output
SimpleXML changing parsed decimals
can't navigate SimpleXMLElement data
PHP SimpleXML Load File with Null elements
Iterating through and editing a SimpleXML object by reference in PHP
Simplexml Insert into sub element
warning: simplexml_load_file(): I/O warning : failed to load external entity drupal [closed]
What's wrong with this code? SimpleXMLElement [closed]

Categories

HOME
twitter
hook
minimum-spanning-tree
filtering
amortized-analysis
youtube-api-v3
jgroups
ip
add
dacpac
facebook-php-sdk
wheelnav.js
numeral.js
php-7.1
commonmark
ibm-odm
caml
clearcase-ucm
telephony
connection-string
nas
autocad-plugin
scriptcs
mmap
h2db
brunch
fluentvalidation
compatibility
http-status-code-503
fog
code-contracts
wallpaper
amazon-kinesis-kpl
web-mining
abstract-class
ansible-playbook
webdriver-manager
particles.js
spring-security-kerberos
nxlog
no-www
outlook-api
flashair
node-sass
.net-4.6.2
segment
executenonquery
flickr-api
parentheses
zip4j
errordocument
veracode
flow-control
mcafee
webdriverjs
minimization
topbeat
time-and-attendance
sts-springsourcetoolsuite
transmitfile
essence
testng-dataprovider
bluegiga
pundit
riak-cs
service-accounts
graph-api-explorer
fscommand
dd
java.util.concurrent
document-classification
openexr
yorick
valuechangelistener
typo3-neos
stxxl
gridfs
soundtouch
odata4j
tws
back-stack
comaddin
sublist
multipage
objective-c-2.0
getmessage
osql
boost-filesystem
gnustep
genshi
infobox
javax.script
recent-documents
file-comparison
krl
coda-slider
infrastructure
vc90
perfect-hash
sef
photoshop-cs4
dbisam
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App