最終変更時刻を取得するためだけにhttpリクエストを送信することは可能ですか?

StackOverflow https://stackoverflow.com/questions/1806686

  •  05-07-2019
  •  | 
  •  

質問

ファイル自体の代わりに

編集

PHPのデモで最適ですか?

役に立ちましたか?

解決

<?php
// Get Headers
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,            'http://example.com/'); 
curl_setopt($ch, CURLOPT_HEADER,         true); 
curl_setopt($ch, CURLOPT_NOBODY,         true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT,        10); 
$response = curl_exec($ch); 

// Process Headers
$headerLines = explode("\r\n", $response);
foreach ($headerLines as $headerLine) {
    $headerLineParts = explode(': ', $headerLine);
    if (count($headerLineParts) >= 2) {
        $headers[$headerLineParts[0]] = $headerLineParts[1];
    }
}

echo $headers['Last-Modified'];
?>

他のヒント

はい。 &quot; HEAD&quot;メソッドは応答ヘッダーのみを返し、実際のデータは返しません。

phpのget_headers関数を使用できます

$a = get_headers('http://sstatic.net/so/img/logo.png');
print_r($a);

HTTPリクエストにこれらのヘッダー属性を追加する必要があります 304(最終変更)

  1. If-Modified-Since
  2. If-None-Match
  3. If-Unmodified-Since

Andreiは正しいです。HEADはヘッダーのみを取得します。私の提案は、条件が満たされた場合、ヘッダーのみを返し、本文は返しません。コンテンツが更新されている場合、本文には新しい情報が含まれます。

httpリクエストを処理し、リクエストされたファイルの最終変更日を抽出し、応答で日付を返すサービスを作成する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top