Pat*_*and 21
你可以用cURL巧妙地做到这一点:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
// This changes the request method to HEAD
curl_setopt($ch, CURLOPT_NOBODY, true);
// grab URL and pass it to the browser
curl_exec($ch);
// Edit: Fetch the HTTP-code (cred: @GZipp)
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// close cURL resource, and free up system resources
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
Vol*_*erK 18
作为curl的替代方法,您可以使用http上下文选项将请求方法设置为HEAD.然后使用这些选项打开(http包装器)流并获取元数据.
$context = stream_context_create(array('http' =>array('method'=>'HEAD')));
$fd = fopen('http://php.net', 'rb', false, $context);
var_dump(stream_get_meta_data($fd));
fclose($fd);
Run Code Online (Sandbox Code Playgroud)
另见:
http://docs.php.net/stream_get_meta_data
http://docs.php.net/context.http
甚至比 curl 更容易 - 只需使用 PHPget_headers()函数,该函数返回您指定的任何 URL 的所有标头信息的数组。检查远程文件是否存在的另一种真正简单的方法是使用fopen()并尝试以读取模式打开 URL(您需要为此启用 allow_url_fopen)。
只需查看这些函数的 PHP 手册,它就在那里。
| 归档时间: |
|
| 查看次数: |
9075 次 |
| 最近记录: |