Simplexml_load_file issues caused by proxy - php

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.

Related

XML File Gzip and PHP

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)

Simple_HTML_dom and Curl stopped working on a server

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
?>

Using php to open a url ending in .rss

I'm trying to load the data stored at
https://www.nextbigsound.com/charts/social50.rss using php.
I've tried using a curl and the simplexml_load_file function but neither of these are working for me. I'm pretty new to the language but the data that is displayed when I follow this link looks like nothing more than an xml document. However whenever I try to load the url, I get an error saying that it failed to load. Any ideas what I'm doing wrong?
Edit:
This is the line I tried with the simplexml_load_file
$rss = simplexml_load_file('https://www.nextbigsound.com/charts/social50.xml');
When I tried the curl method I used this:
$url = 'https://www.nextbigsound.com/charts/social50.xml';
$ch = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rss = curl_exec($curl);
curl_close($curl);
To check if results were returned I just ran a simple var_dump($rss) but it always showed up as a boolean set to false.
Simple file_get_contents will work
$content = file_get_contents('https://www.nextbigsound.com/charts/social50.rss');
echo $content;
or with use of simple_xml_load_string
$data = file_get_contents('https://www.nextbigsound.com/charts/social50.rss');
$xml = simplexml_load_string($data);
The above code assumes that there is a https wrapper, you can check it by this:
echo 'https wrapper: ', in_array(stream_get_wrappers(), $w) ? 'yes':'no', "\n";
if is output false you need to alow php_openssl extension. This can be done by uncomenting line in your php.ini
extension=php_openssl.dll // on windows machine
extension=php_openssl.so // on unix machine

Read xml/rss-file from Wordpress

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

PHP: send POST request then read XML response?

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);

Categories