[英]How to check if there is anything at URL?
Part of my site requires user to input URLs, but in case they type the URL incorrectly or just input a non-existent one on purpose I end up with a bad record on my database. 我的网站的一部分要求用户输入URL,但是如果他们输入错误的URL或只是故意输入一个不存在的URL,我最终会在数据库中记录错误。
This could be solved by checking the URL to see if there is anything, I can only think of one which is loading the external URL in a PHP file and then parsing it's content. 这可以通过检查URL以查看是否存在来解决,我只能想到一种将外部URL加载到PHP文件中,然后解析其内容的方法。 But I hope there is a method that doesn't put unneeded strain on my server.
但是,我希望有一种方法不会对服务器造成不必要的压力。
What other ways exist to check if there is anything at a particular URL? 还有什么其他方法可以检查特定URL是否存在任何内容?
I would just make a HEAD
request. 我只会提出
HEAD
请求。 This will work with most servers, and avoids downloading the entire page, so it is very efficient. 这将适用于大多数服务器,并且避免下载整个页面,因此非常高效。
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
All you have to do is parse the status code returned. 您要做的就是解析返回的状态码。 If it is 200, then you're good.
如果是200,那么您就很好。
Example implementation with cURL here: http://icfun.blogspot.com/2008/07/php-get-server-response-header-by.html 在此处使用cURL的示例实现: http : //icfun.blogspot.com/2008/07/php-get-server-response-header-by.html
您可以使用php get_headers($ url) ,如果没有答案,它将返回false
if you're willing to include a tiny Flash embed you can do a crossdomain AJAX call from the client to see if anything useful is at the destination. 如果您愿意包含一个很小的Flash嵌入,则可以从客户端进行跨域AJAX调用,以查看目标处是否有有用的东西。 This would alleviate any Server involvement at all.
这将完全减轻服务器的参与。
http://jimbojw.com/wiki/index.php?title=Introduction_to_Cross-Domain_Ajax http://jimbojw.com/wiki/index.php?title=Introduction_to_Cross-Domain_Ajax
I would use cURL to do this, that way you can specify a timeout on it. 我将使用cURL来执行此操作,这样就可以在其上指定超时。
See the comments on: http://php.net/manual/en/function.get-headers.php 请参阅以下评论: http : //php.net/manual/en/function.get-headers.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.