Bogdan - 

Sorry for the delay to your follow-up.

The total script is below... first let me explain a little more of what I'm
doing for clarity's sake.

I'm downloading a lot of data files from a site. The way the site works, you
supply some information in a form regarding the data set you want. This goes
to a PERL script which creates a packaged (zipped) data set and puts it in
an FTP directory.

What I wanted to do was write a script that would automatically do the form
submission, retrieve the name and location of the zip file, and save it to
my local hard disk. I happened to have a list of over 1000 data set names
that I wanted to get... so I just needed to loop through them.

FWIW, the "faking a form post" part of this, which is really the core of
what I'm doing, is based on the following in the pgp_general archives:

  http://marc.theaimsgroup.com/?l=php-general&m=101899239727991&w=2

You can search for this by searching the archives for the term "asphttp".
ASPHTTP is a tool for ASP that does the same thing... only it costs money,
and this little function is free! How cool is that?

Here's the script (got it working, BTW).

Chip

<?
/*--- PHP Script --------------*/
$data = array("dataset1", "dataset2", ... , "dataset1000");

//Loop through each value in the array, POSTING data, and downloading
// the dataset to the hard drive.
foreach($data as $a) {
  //Since this script is going to take a long time to execute
  // we need to set the max time limit to some really high 
  // number so that it doesn't time out. This probably needs
  // to be done on the web_server as well (e.g. the def time-out
  // in IIS is 300 seconds).
  set_time_limit(120000);

  //The next two variables make up the location of the resource
  // that you will be posting to. This is basically what would
  // appear in the ACTION section of a FORM element. In this case:
  //  http://www.someserver.com/cgi-bin/some_script.pl
  $host="www.someserver.com;
  $path="/cgi-bin/some_script.pl";

  //The next line makes up the 'data to send' in the form. Apparently
  // if you start the string with a "&", this will use the POST method,
  // while if you start with a "?" it will use the GET method (or at
  // least it's acting like it's doing one or the other).
  $data_to_send = "&myDataSetName=" . $a .
"&FormVar1=Value1&FormVar2=Value2";
  PostToHost($host, $path, $data_to_send);
}

function PostToHost($host, $path, $data_to_send)
{
  //This part of the function creates a file that essentially POSTs the
  // $data_to_send information to the $host . $path location, and retrieves
  // the information into a variable called $returnData
  $returnData="";
  $fp = fsockopen($host,80);
  fputs($fp, "POST $path HTTP/1.1\n");
  fputs($fp, "Host: $host\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  fputs($fp, "Content-length: ".strlen($data_to_send)."\n");
  fputs($fp, "Connection: close\n\n");
  fputs($fp, $data_to_send);

  //Loop through the variable $returnData line-by-line. In this case,
  // I'm searching for a keyword. The name of the resulting file is always
  // on a line like this:
  //  YOURFTPFILENAME = some_file_name_xxxx.xxxx
  //Once I hit this line, I want to save the entire line into a variable
  // called $filename, and break out of the loop.
  while(!feof($fp)) {
    $returnData=fgets($fp, 128);
    $filename = strstr($returnData, 'YOURFTPFILENAME');
    if ($filename)
      break;
  }

  //Use some simple string parsing functions to strip out the
"YOURFTPFILENAME = "
  // part of the filename.
  $filename = substr($filename, 18);
  $filename = trim($filename);

  //Now, I'm going to connect to and read the file that was created. In this
case
  // it is always saved to the same location, so all I need to do is specify
the
  // file name. The file will be read into a variable called $contents.
  $host = "http://www.someserver.com/ftp_downloads/images/";;
  $fp2 = fopen($host . $filename, "rb+");
  $contents = fread ($fp2, 25000000); //use some rediculously large number
in fread
                                      // to ensure that you get the whole
thing.

  //make a file to write to on the local system, and dump $contents into the
new
  // file.
  $fp3 = fopen("D:\\myFiles\\SomeDirectory\\" . $filename, "w");
  fputs($fp3, $contents);

  //close all of the documents
  fclose($fp);
  fclose($fp2);
  fclose($fp3);
}
/*---- End of Script ---------------*/
?>

> -----Original Message-----
> From: Bogdan Stancescu [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 22, 2002 10:29 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Re: Automatic Download using URL, HELP!
> 
> 
> Just out of curiousity, how did you ever get PHP to prompt you where to 
> save a file? Could you post a more complete code sample?
> 
> Bogdan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to