curl-and-php
Re: a download
Date: Fri, 07 Dec 2007 10:34:36 -0800
Oops,
After I sent my last message I noticed a problem with my code. I wasn't
properly encoding the post data. The fixed version is below.
<?php
// Set parameters.
$url = "https://www.linkpointcentral.com/lpc/servlet/LPCReport";
$certificateFile = 'C:/php/curl-ca-bundle.crt';
$cookiesFile = 'C:/windows/temp/cookies.txt';
$outputFilename = 'C:/output_file.xsl';
// Set up the post data.
$postData = array(
'pageno' => '1',
'expflag' => '1',
'rptaction' => 'qryordertime',
// ... etc.
);
$encodedPostData = array();
foreach ($postData as $key => $value) {
$encodedPostData[] = $key.'='.htmlentities(urlencode($value));
}
$postFields = implode('&', $encodedPostData);
print('Post fields: '.$postFields."\n");
// Create the curl handle with the data to send.
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
// Set other options.
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CAINFO, $certificateFile);
curl_setopt($ch, CURLOPT_CAPATH, $certificateFile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesFile);
// Get the result and output.
$result = curl_exec($ch);
// Output the result to a file.
if (file_put_contents($outputFilename, $result) === false) {
print('Failed to write file contents to '.$outputFilename."\n");
}
// Close the handle.
curl_close($ch);
?>
I hope *that* helps. :-)
Morgan Galpin.
Morgan Galpin wrote:
> Hi Colleen,
>
> I think what might be happening with the files you're trying to download
> is that the server is setting certain http header values to indicate to
> your web browser that it should download the file rather than display
> the file. Every link that you click on in a web browser simply downloads
> the file that is linked to. The browser decides whether or not to
> display the file or to ask the user to save it somewhere. Browsers know
> how to display html, text, and images, which is why they never ask about
> those. When the server wants to force the client browser to save the
> file rather than display it, because the browser will try to display an
> xsl file, it will include a header like, 'Content-Disposition:
> attachment; filename="somefile.xsl";'. When the client browser receives
> a file with a header like this, it will normally display a "Save As"
> dialog where the user can choose to save the file.
>
> So, there should be no difference for trying to save the file received
> in this manner with curl versus any other URL. I don't know what your
> code looks like, but I would try something like the following:
>
> <?php
>
> // Set parameters.
> $url = "https://www.linkpointcentral.com/lpc/servlet/LPCReport";
> $certificateFile = 'C:/php/curl-ca-bundle.crt';
> $cookiesFile = 'C:/windows/temp/cookies.txt';
> $outputFilename = 'C:/output_file.xsl';
>
> // Set up the post data.
> $postData = array(
> 'pageno' => '1',
> 'expflag' => '1',
> 'rptaction' => 'qryordertime',
> // ... etc.
> );
> $postFields = implode('&', $postData);
>
> // Create the curl handle with the data to send.
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_POST, true);
> curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> curl_setopt($ch, CURLOPT_URL, $url);
>
> // Set other options.
> curl_setopt($ch, CURLOPT_VERBOSE, true);
> curl_setopt($ch, CURLOPT_CAINFO, $certificateFile);
> curl_setopt($ch, CURLOPT_CAPATH, $certificateFile);
> curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
> curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesFile);
> curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesFile);
>
> // Get the result and output.
> $result = curl_exec($ch);
>
> // Output the result to a file.
> if (file_put_contents($outputFilename, $result) === false) {
> print('Failed to write file contents to '.$outputFilename."\n");
> }
>
> // Close the handle.
> curl_close($ch);
>
> ?>
>
> Let us know if that helps,
> Morgan Galpin.
>
>
> Colleen R. Dick wrote:
>
>> I have managed to log into a site and make it do a query and have my
>> custom data appear on a dynamic web page (if I choose to print out the
>> results of curl_exec.)
>> There is a BUTTON on that page that allows you to download a spreadsheet
>> of that data. You can't just fetch the file with wget or curl because
>> I'm sure it doesn't live anywhere under document root so it wouldn't
>> have a URL. you have to submit the form to ask the server for it and it
>> downloads it as some sort of attachment with a dialog when you do it
>> manually. Obviously I want to set it up so it just falls into some
>> file handle. I checked what the button does, it just calls a
>> javascript to set up some other post variables before submitting the
>> form.
>>
>> Here is the submit button:
>>
>> <input class="button" type="button" value="Export All Data"
>> onclick="exportFile(); return(false);
>>
>> Here is the javascript exportFile:
>>
>> function exportFile()
>> {
>> document.RptForm.pageno.value = 1;
>> document.RptForm.expflag.value = 1;
>> document.RptForm.rptaction.value = "qryordertime";
>>
>> document.RptForm.pagefile.value = "temp1196982492309.1040408.1";
>>
>> exptype = document.RptForm.exptype.options[document.RptForm.exptype.selectedIndex].value;
>> if (exptype=="csv") document.RptForm.expfile.value="exp_ordercsv.xsl";
>> else if(exptype=="xml") {
>> document.RptForm.target = "_blank";
>> document.RptForm.expfile.value="exp_orderxml.xsl";
>> }
>> document.RptForm.submit();
>> document.RptForm.target = "_self";
>> }
>>
>> So what I did was hard code all the post variables like the javascript does them
>> and submits the form.
>> It doesn't throw any errors, I don't think. I made it print out curl_getinfo
>> Array
>> (
>> [url] => https://www.linkpointcentral.com/lpc/servlet/LPCReport
>> [content_type] => text/html; charset=ISO-2259-1
>> [http_code] => 200
>> [header_size] => 153
>> [request_size] => 665
>> [filetime] => -1
>> [ssl_verify_result] => 0
>> [redirect_count] => 0
>> [total_time] => 2.981608
>> [namelookup_time] => 0.000101
>> [connect_time] => 0.113942
>> [pretransfer_time] => 0.529531
>> [size_upload] => 0
>> [size_download] => 0
>> [speed_download] => 0
>> [speed_upload] => 0
>> [download_content_length] => 0
>> [upload_content_length] => 0
>> [starttransfer_time] => 2.981528
>> [redirect_time] => 0
>> )
>>
>> It names the document rpt*******.csv but I would like it to download into a file name of my choice.
>> To that end I have set up and opened a filehandle but it always ends up of size zero, which makes sense if
>> I don't know how to tell CURL to use it. Where is the document going if anywhere?
>> It is not in tmp or in my web tree. I'm sorry if I am stupid but I have really looked very hard for the answer.
>> All I see is little examples to use Curl to download a file where it has a URL. I know how to do that.
>>
>> _______________________________________________
>> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>>
>>
> _______________________________________________
> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2007-12-07