1

I have a large database of remote mp3 urls of various websites both old and new. I am using the following code to check whether the urls are valid mp3 or not in php

function check_url($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);


    if($headers['content_type']=='audio/mpeg'&&$headers['http_code']=='200')
        return 1;
    else    
        return 0;
}

It is consuming huge execution time. Can anybody suggest any other alernative to perform this task.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Rakesh kumar
  • 392
  • 5
  • 13
  • This is a bit vague. Have you checked your code to see where it is consuming the huge execution time? – Robert Harvey Jul 07 '15 at 16:38
  • 10
    If I get you right, then cURL is not the problem here. If you really only want to check if the URLs are valid, and you are not interested in the content of the files, then you should change the code to only perform HEAD requests instead of downloading the files. That gives you information about file existance and mime type. Which is all you need. – arkascha Jul 07 '15 at 16:40
  • thank you arkascha can u tell me how exactly can I get only the headers without the content . I am very new to cURL – Rakesh kumar Jul 07 '15 at 16:49
  • 1
    http://stackoverflow.com/a/13861887/2433843 – Francesco de Guytenaere Jul 07 '15 at 16:51
  • 1
    In the link @Ciccio refers to the crucial line is `curl_setopt($ch, CURLOPT_NOBODY, 1);` which switches the http method from `GET` to `HEAD` - as always see [the fine docs](http://php.net/manual/en/function.curl-setopt.php) – fvu Jul 07 '15 at 16:59

1 Answers1

3

we can use a php built in function

$headers=get_headers($url);
print_r($headers);

returns the following

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Tue, 07 Jul 2015 16:53:22
    [2] => Server: nginx/1.4.1
    [3] => Last-Modified: Tue, 19 Oct 2010 22:06:24 GMT
    [4] => ETag: "4cbe1660-7807f6"
    [5] => Accept-Ranges: bytes
    [6] => Content-Length: 3493158
    [7] => Connection: close
    [8] => Content-Type: audio/mpeg
)
Rakesh kumar
  • 392
  • 5
  • 13
  • The thing is, by default `get _headers` will use GET method, and you won't gain anything. How to use a stream context, as [explained in the documentation](http://php.net/manual/en/function.get-headers.php) to use the `HEAD` method should be part of your answer. – fvu Jul 07 '15 at 17:02
  • By default this sends a `GET` request. See http://php.net/manual/en/function.stream-context-set-default.php on how to set it to head – Francesco de Guytenaere Jul 07 '15 at 17:02