0

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

2
  • What do you mean by "Text (ASCII)" format here and what by "binary"? Please clarify. Are you familiar with content-types ? Commented Oct 13, 2010 at 0:00
  • Please look to my comment below. The links I'm checking are either playlists for audio streams or streams themselves. I need the way to tell if the link returns text data (playlist) or the stream itself which is binary data. Commented Oct 13, 2010 at 17:57

1 Answer 1

1

Look at the content-type header

...
$response = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
var_dump($info);
Sign up to request clarification or add additional context in comments.

5 Comments

This approche only gives me the
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).
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.
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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.