I'm try to read an external XML file with a PHP script, no wmatter what I try I get an "empty document" error. If I open the url in my browser I can access and read the xml fine.
There are numerous other posts on stack overflow with similar problem as mine but none of the solutions work in my case.
This is my code:
$url="http://xml.example.com";
$xml = new SimpleXMLElement("compress.zlib://$url", NULL, TRUE);
parser error : Document is empty in /home/admin/public_html/xml/index2.php on line 4
Apparently the 3rd party service requires that I explicitly request gzip compression.
Any ideas?
Thanks,
Alan.
Ok I got it working using curl then createing an XML object (I think!)
$ch = curl_init();
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
curl_close($ch);
//print_r($output);
$oXML = new SimpleXMLElement($output);
The results are compatible with my script and can be parsed to extract the data :0)
Related
This is the cURL setup I am using:
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, 'url to xml file');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,400);
curl_setopt($ch, CURLOPT_HEADER, 0);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
And for some reason I get the following error:
[error] [php] simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found (/path/to/rendered/file/file.php
When I call the xml url in the browser I get valid xml so what is causing this error?
Kind regards,
Pim
You should check $output variable contents. Just do:
echo $output;
and make sure it has your XML document. Generally if some error has occurred cURL will return you a document containing the error and thus you would be able to see why it fails.
Also using curl_errno() function makes sense: it should be 0 if everything is OK with request.
There is a supermarket website and I need to get list of product name and price data.
The website is: http://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler
However, I cannot get this content with success. Every attempt finalized with a null result. I am not familiar with cURL, but it is recommended me to overcome this issue. As I see, the product list is called with Ajax - JSON and for this reason, I should follow requests to see JSON files and their contents using PHP. ...But how?
Thank you in advance.
The code I tried:
<?php
$url="https://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
Your curl request did work and you are getting html response in the $result variable. The problem is that you are treating the html response string like a valid JSON string.
instead of
var_dump(json_decode($result, true));
try
var_dump($result);
Here $result is not a valid JSON string. It is a string containing the html that the server responded. So you cannot parse it directly into an array or object without using a html parser.
I've built a script locally which works fine. I've now moved that script onto a server that's behind a proxy and I've come into some issues.
Here's the code:
$yahooXML = simplexml_load_file('http://query.yahooapis.com/v1/public/yql?q=select+*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22'.$from.''.$to.'%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
print_r($yahooXML);
die();
I'm getting a failed to open stream and I/O warning : failed to load external entity error using this.
I explored using cURL to load the data and then parse with simplexml but not sure if this is possible?
any ideas?
Edit:
I loaded the page with CURL which failed as well so I added the proxy option in and it fixed it. Now I just need to load this with XML?
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_PROXY, 'proxysg.uwe.ac.uk:8080');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$feed = 'http://query.yahooapis.com/v1/public/yql?q=select+*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22'.$from.''.$to.'%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
$data = curl($feed);
echo $data;
die();
Once you have the XML file and you've verified that it's proper XML, you can load it into php via simplexml_load_string() or simplexml_load_file() depending on what you have.
If your $data var is a string w/well formed XML, then:
$xml = simplexml_load_string($data);
print_r($xml);
should work just fine. Of course, now you have a simple xml object, which will work with any of the normal simplexml functions.
I've got a site "mysite.com", this has a rss/xml-feed located at mysite.com/feed.
I have to read this feed in a php-script but simplexml_load_file returns an empty result. How can I get the "real" path to the feed? I assume this file is created using some clever .htaccess and such.
I think it has nothing to do with .htaccess. to get the feed you have two options.
one is using file_get_content, the other is using cURL.
$xml = file_get_contents('http://mysite.com/feed');
and
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://mysite.com/feed');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
now the $xml hold the xml file, so you can use simplexml_load_file to parse it.
An alternative is you can use Rss PHP. more details are here
I'm trying to write a PHP script that sends a POST request to a remote server, then parses the XML response.
I can do the POST request, but am having difficulty (from other SO questions) working out how to parse the XML response.
My current code gives me: Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "1" in /Users/simon/usercreate.php on line 46 - the simplexml_load_file($response) line.
I'm working on a local server, not sure if that makes a difference. Code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$response = curl_exec ($curl);
curl_close ($curl);
echo $response;
$rxml = simplexml_load_file($response);
echo $rxml->title;
What am I doing wrong?
use simplexml_load_string instead of simplexml_load_file
You have to set the cURL option to return the transfer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Instead of loading a file, you want to load a string.
// Instead of
$rxml = simplexml_load_file($response);
// You want
$rxml = simplexml_load_string($response);