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);
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.
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)
I've had several scripts pull information from a different server work perfectly for about a year.
Recently, it looks like they've changed something and both simple_html_dom and curl fail to work, either resulting in a failed to open stream: HTTP request failed! error or simply freezing.
Sometimes, I can successfully get ONE request through, but only one. The problem is the nature of the script requires more than one request from that server.
The page I am trying to pull is http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/cntnBscSrch.do?textField1=otimo&selectField1=tmlookup_ext&useblg=bscSrch.do%3Flang%3Deng&languageDirection=f&lang=eng&submitButton=Search&selectMaxDoc=1000000&selectDocsPerPage=1000000
Would really really appreciate any help
This is the simplified version of the code which also results in the same problem:
<?php
require("simple_html_dom.php");
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$link="http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/cntnBscSrch.do?textField1=otamo&selectField1=tmlookup_ext&useblg=bscSrch.do%3Flang%3Deng&languageDirection=f&lang=eng&submitButton=Search&selectMaxDoc=1000000&selectDocsPerPage=1000000";
$html = file_get_html($link);
echo "SIMPLE HTML DOM:<br>".$html;
$html = file_get_contents_curl($link);
echo "CURL:<br>".$html
?>
In the bellow snipet you can see that i am trying to download some data from a website via their API in JSON format.
My problem is that when i try to get the data in json format (25 mb of text data) the php script send me a 200 response (which you can see below). But the weird part is that the script actualy finishes executing and the response is downloaded by my browser as a file.
The url if inputed into browser returns correct data, even via wget i can download the contents from this url.
Could it be that curl will pass through the respose from the server to my browser?
$ch = curl_init("https://example.com/api/json/view/view_name?authtoken=xxxyyyaaaa&raw=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec ($ch);
downloaded file "myscriptname.php" which contains:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>200 OK</title>
</head><body>
<h1>OK</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>
Well this is what the cURL optin CURLOPT_RETURNTRANSFER you are using actually is for: to return the transferred content to the user agent.
Your request was OK , but server returned the content as html page (may be their API is not working) so you are getting the maintenance page as reply.
looking at php.net
CURLOPT_RETURNTRANSFER TRUE
to return the transfer as a string of the return value of curl_exec()
instead of outputting it out directly.
when i used curl to
<?php
$ch = curl_init("http://localhost/testjson.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec ($ch);
print_r($data);
?>
what i got after exec is
{"result":["google","stack","facebook"]}
(this is because i printed the result using print_r($data);)
testjson.php is
<?php
$data = array ('result'=>array('google','stack','facebook'));
header('content-type:application/json');
echo json_encode($data);
?>
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.