Jump to content

Using cURL for Currency Parse Feed


Recommended Posts

I've been searching for alternatives to allow_url for my currency real live update cause my host disabled the function. I found the following script but not sure how to apply it in prestashop. Some care to assist me?

class XMLWrapper {

public function loadXML($url) {
if (ini_get('allow_url_fopen') == true) {
return $this->load_fopen($url);
} else if (function_exists('curl_init')) {
return $this->load_curl($url);
} else {
// Enable 'allow_url_fopen' or install cURL.
throw new Exception("Can't load data.");
}
}

private function load_fopen($url) {
return simplexml_load_file($url);
}

private function load_curl($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return simplexml_load_string($result);
}

}
Link to comment
Share on other sites

  • 1 year later...

Hi,

here is how to use it (in classes/currency.php):

   static public function refreshCurrencies()
   {
               $curl = curl_init('http://www.prestashop.com/xml/currencies.xml'); 
               curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
               $result = curl_exec($curl); 
               curl_close($curl); 
               /*return simplexml_load_string($result);
       if (!$feed = @simplexml_load_file('http://www.prestashop.com/xml/currencies.xml')) */
               if (!$feed = @simplexml_load_string($result))
           return Tools::displayError('Cannot parse feed!');



With that change, you don't have to enable "allow_url_fopen" in your php.ini (which is a big security risk). However, curl need to be installed:)

With the code you provided, PS developper could test if curl is installed and use that code instead of using simplexml_load_file. I may submit a patch if I get some time:)

I hope it will help some persons.

Regards

Link to comment
Share on other sites

×
×
  • Create New...