I am developing a script for a music company in PHP that has different servers so they need to display a file if it exists or not on the external server
like they have 3 versions of each music file mp3 mp4 etc ..... and they are accessing the files (each version ) from there specific external server . i have made three solutions for it all of them worked like charm but they are making the server slow .
First Method :
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* Handle 404 here. */
}
curl_close($handle);
/* Handle $response here. */
Second Method : Using NuSOAP i made an api which checks internally the file and returns yes/no
Third Method :
function checkurl($url)
{
return true;
$file_headers = @get_headers($url);
//var_dump($file_headers);
if($file_headers[0] == 'HTTP/1.1 302 Moved Temporarily' || $file_headers[0] =='HTTP/1.1 302 Found') {
$exists = false;
}
else {
$exists = true;
}
return $exists;
}
So i need a solution that doesn't makes the server slow any suggestions
Be sure to issue a HEAD request, not GET , since you don't want to get the file contents. And maybe you need to follow redirects, or not...
Example with curl (thanks to this blog post ):
<?php
$url = 'http://localhost/c.txt';
echo "\n checking: $url";
$c = curl_init();
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $c, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $c, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
curl_setopt( $c, CURLOPT_HEADER, 1 );
curl_setopt( $c, CURLOPT_NOBODY, true );
curl_setopt( $c, CURLOPT_URL, $url );
$res = curl_exec( $c );
echo "\n\ncurl:\n";
var_dump($res);
echo "\nis 200: ";
var_dump(false !== strpos($res, 'HTTP/1.1 200 OK'));
SOAP or other web service implementation can be an option if the file is not available by HTTP.
If you want to use get_headers(), please note that by default it's slow because it issues a GET request . To use HEAD request, you should change the default stream context (please check get_headers() on php manual):
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
I thought it works with above answers but it wasnt working where there were too many requests so i finally try again and again and found this solution its working perfectly actually the problem was redirects too many of them so i set time_out 15 in curl and it worked
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$r = curl_exec($ch);
$r = split("\n", $r);
var_dump($r);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.