|
stream_get_meta_data
Retrieves header/meta data from streams/file pointers
(PHP 4 >= 4.3.0, PHP 5)
Code Examples / Notes » stream_get_meta_dataed
Below is a function I wrote to pull the "Last-Modified" header from a given URL. In PHP version 4.3 and above, it takes advantage of the stream_get_meta_data function, and in older version it uses a conventional GET procedure. On failure to connect to $url, it returns NULL. If the server does not return the Last-Modified header, it returns the current time. All times are returned in PHP's integer format (seconds since epoch). Use it as so: $last_modified = stream_last_modified('http://www.php.net/news.rss'); if (!is_null($last_modified)) if ($last_modified < time()-3600) //Older than an hour echo 'URL is older than an hour.'; else echo 'URL is fairly new.'; else echo 'Invalid URL!'; function stream_last_modified($url) { if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0) { if (!($fp = @fopen($url, 'r'))) return NULL; $meta = stream_get_meta_data($fp); for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) { if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')) { $modtime = substr($meta['wrapper_data'][$j], 15); break; } } fclose($fp); } else { $parts = parse_url($url); $host = $parts['host']; $path = $parts['path']; if (!($fp = @fsockopen($host, 80))) return NULL; $req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n"; fputs($fp, $req); while (!feof($fp)) { $str = fgets($fp, 4096); if (strstr(strtolower($str), 'last-modified')) { $modtime = substr($str, 15); break; } } fclose($fp); } return isset($modtime) ? strtotime($modtime) : time(); } |
Change Languagestream_bucket_append stream_bucket_make_writeable stream_bucket_new stream_bucket_prepend stream_context_create stream_context_get_default stream_context_get_options stream_context_set_option stream_context_set_params stream_copy_to_stream stream_encoding stream_filter_append stream_filter_prepend stream_filter_register stream_filter_remove stream_get_contents stream_get_filters stream_get_line stream_get_meta_data stream_get_transports stream_get_wrappers stream_register_wrapper stream_resolve_include_path stream_select stream_set_blocking stream_set_timeout stream_set_write_buffer stream_socket_accept stream_socket_client stream_socket_enable_crypto stream_socket_get_name stream_socket_pair stream_socket_recvfrom stream_socket_sendto stream_socket_server stream_socket_shutdown stream_wrapper_register stream_wrapper_restore stream_wrapper_unregister |