问题
I am trying to build a script where it downloads a file using the Zend http client: http://framework.zend.com/manual/en/zend.http.html but I can't find anywhere where it says how to do this so I'm wondering if its possible... The file is dependent on being logged in so I need to have it done through the zend http client so it can make use of the cookies that are created when the script logs in..
any advice is greatly appreciated
回答1:
Complete the request for the file just like you would a webpage. The body of the response should contain the binary data for the file (or possibly text data if you were downloading a CSS, XML, etc... file).
$body = $response->getBody();
file_put_contents("myfile.zip",$body);
回答2:
Example #11 Receiving file from HTTP server with streaming
$client->setStreaming(); // will use temp file
$response = $client->request('GET');
// copy file
copy($response->getStreamName(), "my/downloads/file");
// use stream
$fp = fopen("my/downloads/file2", "w");
stream_copy_to_stream($response->getStream(), $fp);
// Also can write to known file
$client->setStreaming("my/downloads/myfile)->request('GET');
http://framework.zend.com/manual/en/zend.http.client.advanced.html
last example
回答3:
Personally, I would use cURL instead.
cURL in the PHP manual: http://php.net/manual/en/book.curl.php
A simple example of using cURL to download a file: http://www.webdigity.com/index.php?action=tutorial;code=45
来源:https://stackoverflow.com/questions/3173390/possible-to-download-a-file-through-the-zend-http-client