[英]Checking directory or file from external owned server
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 我正在为PHP的音乐公司开发脚本,该脚本具有不同的服务器,因此他们需要显示文件(如果外部服务器上存在或不存在)
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 . 像他们每个音乐文件mp3,mp4等有3个版本......并且他们从那里特定的外部服务器访问文件(每个版本)。 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 第二种方法 :使用NuSOAP,我制作了一个api,用于在内部检查文件并返回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. 请确保发出HEAD请求,而不是GET ,因为您不想获取文件内容。 And maybe you need to follow redirects, or not...
也许您需要遵循重定向,否则就不需要...
Example with curl (thanks to this blog post ): curl示例(感谢此博客文章 ):
<?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. 如果HTTP无法提供该文件,则可以选择SOAP或其他Web服务实现。
If you want to use get_headers(), please note that by default it's slow because it issues a GET request . 如果要使用get_headers(),请注意默认情况下它很慢,因为它发出GET请求 。 To use HEAD request, you should change the default stream context (please check get_headers() on php manual):
要使用HEAD请求,您应该更改默认的流上下文(请检查php手册上的get_headers()):
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 我认为它可以与上面的答案一起使用,但是在有太多请求的地方它不起作用,所以我终于一次又一次尝试,发现此解决方案非常有效,实际上问题是重定向了太多这样的问题,所以我将time_out 15设置为curl并起作用了
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.