Download a Remote File Using PHP

Hi all,

I'm accessing a remote xml file from my site. This file feeds a section of my page with daily news.

I won't be able due to a hosting issue to do it any more, I won't be able to access the remote file on the spot. So I have to download this xml file to my host and feed my page's section locally. I will check for daily file updates with a cron job.

How can I do that ? How can I download the remote file to my host?

I'm using the code below, but I think I having issues with the storing in my host part because I'm getting a blank page :(

Please notice that the person that serves the remote file has given permission to my IP to do so, therefore no stealing in here.

Thanks a ton
<?php

$url ="http://twitter.com/statuses/user_timeline/70640679.rss"; //working file example, cannot state the real xml here cause you'll need ip permission to access it
$dir  = "C:\/";//how should I write the local host in here? 
$file_name = 'test.xml';

function downloadRemoteFile($url,$dir,$file_name = NULL){
    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file_binary = substr($buffer, - $parts[1]);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($dir . "/" . $file_name,'w');
    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
} 
?>

Open in new window

Dada44Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ante0Commented:
It's just $dir that gives you problems?
Did you try $dir =  "C:";
since you add a / in the fopen function.
0
V4nP3rs13Commented:
I edited your code. Try this. It works for me
<?php

$url ="http://twitter.com/statuses/user_timeline/70640679.rss";
$file_name = 'test.xml';

function downloadRemoteFile($url,$dir,$file_name = NULL){
    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file_binary = substr($buffer, - $parts[1]);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($file_name,'w');
    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
} 
?>

Open in new window

0
Dada44Author Commented:
Thanks a ton for answering.

V4nP3rs13, not working for me,  text.xml is supposed to be in the same folder where the script is ?? then no new file.

Ante0, the problem is that I'm not downloading not saving anything :(

Thanks again
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

V4nP3rs13Commented:
Try this one

<?php

$url ="http://twitter.com/statuses/user_timeline/70640679.rss";
$file_name = 'test.xml';
    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file_binary = substr($buffer, - $parts[1]);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($file_name,'w');
    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
?>

Open in new window

0
Ante0Commented:
Did you chmod (777) the dir the script is in, so it allows files to be saved?
0
V4nP3rs13Commented:
Does it work now?
0
Dada44Author Commented:
no, it did not work :(
0
Dada44Author Commented:
in which can I debug it? how can I test the the parameters are going the right way?
thanks
0
V4nP3rs13Commented:
look... the xml file will be stored in the directory where the script is. It must work. Try again with this one:

<?php

$url ="http://twitter.com/statuses/user_timeline/70640679.rss";
$file_name = 'test.xml';
    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file_binary = substr($buffer, - $parts[1]);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($file_name,'w');
    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dada44Author Commented:
thanks!!!!!!!!
0
V4nP3rs13Commented:
Does it work now?? As you wanted?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.