Fopen problem

software development

#1

Hello,

I have a script that i use that parsing xml data from my affiliate into a db but i need some help because is not working on dreamhost =>

function GetXMLTree($xmlloc){

if (file_exists($xmlloc)){
$data = implode(’’, file($xmlloc));
} else {
$fp = fopen($xmlloc,‘r’);
while(!feof($fp)){
$data = $data . fread($fp, 1024);
}
fclose($fp);

}

Anyone an idea how i can let this code work ?


#2

My guess is that $xmlloc is a url, and DH does not allow the user of fopen to open remote files. There is more info here:

http://wiki.dreamhost.com/Allow_url_fopen

You really should rewrite the script to use cURL.

–rlparker


#3

Yes $xmlloc is a location of an xml file.

But i don’t know how to rewrite that script via curl. A year ago someone else made that script for me but i was on another host then :frowning:


#4

You might try to track down the author of the original script and see if he/she can help you “cURLify” it.

Additionally, you might consider attacking it yourself as a learning experience, using the very nice cURL PHP Tutorial on the DreamHost wiki.

–rlparker


#5

I found a working solution by using curl to create a local xmlfile with the data. see below =>

function GetXMLTree($xmlloc){

$ch = curl_init($xmlloc);
$fp = fopen(“xmlfile.php”, “w”);

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

if (file_exists(“xmlfile.php”)){
$data = implode(’’, file(“xmlfile.php”));
} else {
$fp = fopen(“xmlfile.php”,‘r’);
while(!feof($fp)){
$data = $data . fread($fp, 1024);
}

fclose($fp);
}


#6

Very nice! And I think you will be a lot happier with that in the long run.

Thanks for sharing your code here for others! :slight_smile:

–rlparker