I am trying to turn a XML data from another website into arrays in my program. This is what I have written so far:
<?php
$rss = 'http://headlines.yahoo.co.jp/rss/asahik-dom.xml';
$xml = simplexml_load_file($rss);
var_dump($xml);
?>
However, when I try to load the php page, it comes up with this error:
Warning: simplexml_load_file(http://headlines.yahoo.co.jp/rss/asahik-dom.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://headlines.yahoo.co.jp/rss/asahik-dom.xml" in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11
bool(false)
FYI the $xml = simplexml_load_file($rss); is line 11.
Which part of my code has gone wrong?
Please help.
Please try file_get_contents() to load file and then use SimpleXMLElement to parse it.
Try
$rss = file_get_contents('http://headlines.yahoo.co.jp/rss/asahik-dom.xml');
$xml = new SimpleXMLElement($rss);
print_r($xml);
NOTE allow_url_fopen must be enabled in php.ini
Related
I want to parse xml file from another server.
<?php
$xml = simplexml_load_file('http://example_page.com/api/test.xml');
?>
And this code work only if this file is on this same server as page, but I have got xml file on another server.
Warning from webpage:
Warning: simplexml_load_file(http://example_page.ugu.pl/api/test.xml) [function.simplexml-load-file]: failed to open stream: Connection refused in /virtual/z/y/example_page.ugu.pl/index.php on line 14
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://example_page.com/api/test.xml" in /virtual/z/y/example_page.ugu.ugu.pl/index.php on line 14
P.S. allow_url_fopen is still on.
Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file() doesn't follow redirects...
So the solution would be to use file_get_contents() or cUrl...
As file_get_contents() is easier, I am showing that only...
The code would have to be something like:
<?php
$xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));
?>
More:
<?php
$xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));
?>
^ That too, totally works!
You should use CURL (if is active).
$url = "http://www.you-web-here.com/";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xmlString = curl_exec($curl);
PHP Documentation:
Note:
Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.
So. which version are you used?
Or, you can try like this:
$xml_content = file_get_content('http://example_page.com/api/test.xml');
xml = simplexml_load_string($xml_content);
I am having a problem updating an object in my Parse Cloud DB...
Basically I run the code
include_once '../lib/libraries/Parse/autoload.php';
use Parse\ParseClient;
use Parse\ParseQuery;
use Parse\ParseObject;
ParseClient::initialize('xxx', 'xxx', 'xxx');
$query = new ParseQuery("_User");
$query->equalTo("username", $_COOKIE["username"]);
$user = $query->first();
$user->set("plan", "entrepreneur");
$user->save();
I then get this:
Warning:
file_get_contents(https://api.parse.com/1/classes/_User/byMcqe93lC):
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in /Volumes/Transcend/Google
Drive/Projects/Applandr/lib/libraries/Parse/ParseClient.php on line
240
Warning: Invalid argument supplied for foreach() in
/Volumes/Transcend/Google
Drive/Projects/Applandr/lib/libraries/Parse/ParseObject.php on line
528
I have made sure allow_url_fopen = On is set, and have not tried cURL as it is actually a Parse class that is attempting to get the contents of the URL.
$result = file_get_contents($url, false, $context);
Can somebody please help me, all I want to do is update an object in my Parse DB.
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...)
I am doing this at the moment
<?php
$xml = simplexml_load_file('www.tompeters.com/atom.xml');
print_r($xml);
?>
but it returns this error
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "www.tompeters.com/atom.xml"
I think it is because the feed isn't valid XML?
So how can I validate it so I can parse the feed?
Thanks
Parsing the feed is validating it.
Anyway, the error message looks more like that you don't have access to that file. I don't know PHP, but I would have expected at least
simplexml_load_file('http://www.tompeters.com/atom.xml');
I've got a page on a website that's pulling my favorites feed from YouTube and embedding them into site.
The problem is it's working 80% of the time, but the other 20% of the time i'm getting errors on the page - nothing in the code is changing to cause this, so i'm wondering what might be causing this, or if there's a better way to do what i'm doing...
The Error I'm gettings is a 403 Forbidden when retrieving the XML feed... here's what it looks like (note: the line numbers won't match exactly, because i've simplified the code sample below.
The XML feed in question is here:
https://gdata.youtube.com/feeds/api/users/umarchives/favorites
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/users/umarchives/favorites) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/umarchives/favorites" in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: Invalid argument supplied for foreach() in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 47
Here's the code i'm using:
<?php
// set feed URL
$YouTubeUsername = "umarchives";
$feedURL = "https://gdata.youtube.com/feeds/api/users/".$YouTubeUsername."/favorites";
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->content->attributes();
$videoURL = $attrs['url'];
$videoURL = preg_replace('/\?.*/', '', $videoURL);
$videoURL = str_replace("/v/","/embed/",$videoURL);
$videoTitle = $media->group->title;
echo "<iframe class='youtube-player' width='300' height='225' src='$videoURL'></iframe>\n";
echo "<br>\n";
}
?>
You should be validating the result of $sxml = simplexml_load_file($feedURL); per the Google error validation docs. Then you can print out the actual message that comes along with the 403 code, or possibly decide to retry the request. If it's a random occurrence my guess is a quota limit issue, but the actual error information will likely tell you exactly what you want to know.
MYUSERNAME is not a valid username. Add your own youtube username!
When I call your feed URL in browser (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) I receive this error:
Favorites of requested user are not public.
Make your feed public, and the failure should be gone.