tags:

views:

38

answers:

1

How do I check multiple URLs with PHP. The data returned by from the urls might be in text (ASCII) format or in binary. How do i distinguish which one was returned.

My code has to just detect if the data is binary or text. Nothing else.

Thanks

A: 

Look at the content-type header

...
$response = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
var_dump($info);
zerkms
This approche only gives me the
Ralphz
Content-Type: of downloaded object, NULL indicates server did not send valid Content-Type: header. So its only the value of content type set in headers by the server. I need to somehow recognize if the result is binary data (stream) or text (ASCII).
Ralphz
Well, then it is the only way to guess is to take a little piece of the answer and count the frequency each character occurs within it. After that - analyze which kind of characters they are: ascii-text or some, that cannot be the part of text.
zerkms
I think it would be hard to just guess the content type based on the data stream itself. I think a somewhat usable approach might be to take the extension of the file downloaded into account and guess the type it could be.
Sabeen Malik
@Sabeen Malik: yes, he can, for example, to use `mime_content_type()` - but in this case you have to write the stream into the file, or, `finfo_buffer()`, but in this case you need php 5.3 and PECL fileinfo extension.
zerkms