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);
Related
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
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...)
How do I use PHP to parse XML on the web?
Here is the example XML. How would you get the value of the first "AskRealtime"?
I am aware of SimpleXML. I am looking for the proper code to open the XML webpage, and work with it using SimpleXML.
This suggested code does not succesfully load the page:
$xml = #simplexml_load_file("http://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20AskRealtime%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22A%22%2C%22AA%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
$price = $xml->results->quote[0]->AskRealtime;
echo $price;
Have you tried using SimpleXMLElement? It works just like your code but it's constructed differently.
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20AskRealtime%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22A%22%2C%22AA%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
$xml = new SimpleXMLElement($url, null, true);
echo $xml->results->quote[0]->AskRealtime;
There are different methods for getting the file contents, even though I doubt it's the problem.
/* cURL */
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
curl_close($curl);
/* Alternative */
$contents = file_get_contents($url);
Can you tell what is the error you get? Or try var_dump($xml) and see what it returns.
Step 1. - load the file in SimpleXML $xml = #simplexml_load_file($fl) or die($errorMsg);where $fl is the file URL and $errorMsg is the error message to display.
Step 2. - get a content $whatever=$xml->results->quote->AskRealtime[0]; Explained: it goes to the XML, then what is named "results", then what is named "quote", then what is named AskRealtime, but the first (index 0) of it.
And a note: if, for some reason, you'll have to get something which is named whatever-else (so it has a - in the name), then it works only if you make the code $xml->{'whatever-else'}
Use can use some HTTP request library to get the file, e.g.: http://www.php.net/manual/en/function.http-get.php
Then feed the response to SimpleXML.
I am having a problem with PHP's file_get_contents command.
$url = "http://api.rememberthemilk.com/services/rest/".$format.$auth_token.$filter."&api_sig=".$md5.$apikey.$method;
$content = file_get_contents($url);
$array = json_decode($content, true);
$taskname = $array['rsp']['tasks']['list']['taskseries']['name'];
$duedate = $array['rsp']['tasks']['list']['taskseries']['task']['due'];
($format, $auth_token, $filter, $md5, $apikey, $method are already defined in the script)
When I try to run this code this error is returned:
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad request for line 101
line 101 = $content, = file_get_contents($url);
How to fix this? Thanks!!!
This url does not look great.
http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList
Encode the tokens as follows:
$filter = 'filter='.urlencode( 'dueWithin:"3 days of today"' );
Use urlencode().
Try printing the URL after concatenating the variables. Then paste the URL into the address bar of your browser and see what comes back. Because it's a web service call, your browser might not know what to do with the response. In that case you might get additional information using the command-line user agent "curl", e.g.
curl -v 'http://some-url'
curl is built in to Macs and other *nix machines and is also available for Windows.
I want to read and parse the xml data from an url.My url is:"http://xml.gamebookers.com/sports/bandy.xml".I can access xml data from browser.However,when i attempt to read it by using php it doesnt work.It errors like this:
Warning: file_get_contents(http://xml.gamebookers.com/sports/bandy.xml): failed to open stream: Connection timed out in
How can i fix this?Any comments on this?
Thanks in advance..
Please see here for an answer:
This error is most likely connected to
too many (HTTP) redirects on the way
from your script to the file you want
to open. The default redirection
limit should be 20. As 20
redirects are quite alot there could
be some error in the filename itself
(causing e.g. the webserver on the
other end to do some
spellcheck-redirects) or the other
server is misconfigured or there are
some security measures in place or...
If you feel the need to extend the 20
redirects you could use a stream
context.
$context = array(
'http'=>array('max_redirects' => 99)
);
$context = stream_context_create($context);
// hand over the context to fopen()
$data = file_get_contents('http://xml.gamebookers.com/sports/bandy.xml', false, $context);
// ...
Please see:
Streams
stream_context_create()
HTTP context options
Try the snippet:
$request_url = 'http://xml.gamebookers.com/sports/bandy.xml';
$xml = simplexml_load_file($request_url) or die("feed not loading");
/*
Then just parsing out child node of your xml.
for example
*/
foreach($xml->children() as $child)
{
echo $child->getName().": ".$child."";
}
Hope this help
PS: Open your PHP.INI and look for
allow_url_fopen = On // make sure it is ON