0

I have PHP script with use of Zend_Http_Client.

Script do 2 things:

  • "Expand" shortened urls + get mime-type of URL
  • Get HTML content of text/html urls

I use this simple code:

$client = new Zend_Http_Client($url);
$response = $client->request('GET');
$headers = $response->getHeaders();
$body = $response->getBody();

All OK except situation when URL contains 100Mb MP3 file, for example. I need only HTML content, not MP3. So I want to config Zend_Http_Client to "don't download files more than 2Mb". Is it possible?

Yes, I can make 2 requests - first for get MIME of URL, and second to get HTML if MIME=text/html, but it is too expense to make 2 different requests.

So, question: how to check downloading content size and stop downloading without error when already downloaded content weight became more then 2Mb?

noff
  • 115
  • 1
  • 5

1 Answers1

0

The best practice if you follow the protocol is to send a HEAD request first, check the expected content type and size, and then send a GET request. However, this may not be enough for you and may not be supported by all servers.

Alternatively, you can use the stream response support to abort the download before the body is actually downloaded. See example #11 at http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.streaming

shevron
  • 3,463
  • 2
  • 23
  • 35