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?
 
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
 
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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
 
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
 
Dada44Author Commented:
thanks!!!!!!!!
0
 
V4nP3rs13Commented:
Does it work now?? As you wanted?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.