如何设置 get_meta_tags() & 超时获取标题()
我一直在使用 get_meta_tags() & get_headers() PHP 函数,需要设置一个超时值,以防网站缓慢或无响应。有谁知道该怎么做?
I've been using the get_meta_tags() & get_headers() PHP functions, and need to set a timeout value in case the website is slow or unresponsive. Does anyone know how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够使用
default_socket_timeout
ini 设置来影响这一点(因为它是通过 URL 包装器实现的)。尝试在 php.ini 文件中进行设置,或者设置
10 秒超时(默认值为 60)
You should be able to influence this (as it's via URL wrappers) with the
default_socket_timeout
ini setting.Try either setting it in the
php.ini
file or by doing something liketo set a 10 sec timeout (the default value is 60)
get_headers
和get_meta_tags
函数使用默认的 HTTP 流包装器 下面。您可以更改ini设置为显示在本页其他位置或修改行为该包装器并设置特定的超时:请注意,更改默认 HTTP 流上下文将应用于使用它的所有函数。如果要将超时恢复为原始默认设置,请执行以下操作:
顺便说一句,如果您使用 HTTP Stream Wrapper 调用任何函数,PHP 也会自动填充 变量
$http_response_header
在当前范围内,因此您不必另外调用get_headers
,例如The
get_headers
andget_meta_tags
function use the default HTTP Stream Wrapper underneath. You can either change the ini setting as shown elsewhere on this page or modify the behavior of that wrapper and set a specific timeout:Note that changing the default HTTP Stream Context will apply to all functions using it. If you want to restore the timeout to the original default settings, do:
On a sidenote, if you call any functions using an HTTP Stream Wrapper, PHP will also automatically populate the variable
$http_response_header
in the current scope, so you don't have to callget_headers
in addition, e.g.正如 @Gordon 发布的那样,仅使用 get_headers 执行此操作,但
stream_context_set_default
返回ressource
而不是数组,因此我不确定应该如何将其反馈到同一函数中。它期望一个数组。在 php 7.1 中,
get_headers
添加了第三个参数。所以我想出了这个。当在未过时的 PHP 版本上时,该选项仅添加到 get headers 调用中,否则默认值将保留在脚本执行的其余部分,直到有人向我解释如何在较旧的 PHP 版本上执行此操作。Doing this as posted by @Gordon just with get_headers but
stream_context_set_default
returnsressource
and not a array so I am not sure how I am supposed to feed that back into the same function. It expect a array.In php 7.1 there was a 3rd parameter added to
get_headers
. So I came up with this. When on a not outdated PHP version the option is added to the get headers call only, otherwise the defaults will stick for the rest of the script execution until someone explains to me how to do it on older php versions.@redanimalwar,如何获取默认选项并将其设置回上下文,如下所示:
@redanimalwar, how about getting the default options and setting it back for to the context, something like this: