【问题标题】:Extracting url based on headers with curl基于带有 curl 的标题提取 url
【发布时间】:2012-05-03 09:28:38
【问题描述】:

我想给一个带有 curl 的 url..并根据它的 header 属性 Expires 获取它。

我只想检索过去 30 天内缓存过的页面。

我认为不对的两件事......

1) gmmktime(0, 0, 0, 1, 1, 1998).. 我不知道如何将其设置为今天 - 30 天前。 2)它是否会根据其标题返回我谷歌?如果 url 没有日期超过 30 天的缓存标头,$page 变量将是什么

 function exractURl()
   {
       //How to convert gmmktime to the last 30 days from today
       $ts = gmdate("D, d M Y H:i:s", gmmktime(0, 0, 0, 1, 1, 1998)) . " GMT";
       $c=  curl_init('http://www.google.co.il/');
       curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($c, CURLOPT_HTTPHEADER, array('Expires:'.$ts));
      //  What output will page give me..if the headers arent found
       $page= curl_exec($c);
       curl_close($c);
   }

更新:

   function exractURl()
   {
       $ts = gmdate("D, d M Y H:i:s", strtotime("30 days ago")) . " GMT";
       $c=  curl_init('http://www.google.co.il/');
       curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($c, CURLOPT_HTTPHEADER, array('If-Modified-Since:'.$ts));
       $page= curl_exec($c);
       curl_close($c);
       return $page;
   }

【问题讨论】:

  • 我认为 expires-header 是响应,而不是请求。您可以发送一个“If-Modified-Since”标头。

标签: php curl


【解决方案1】:

您可以使用If-Modified-Since 要求服务器仅在内容发生更改时才返回内容(否则您将收到304 Not Modified 响应)。当然,这取决于服务器的行为。更多详情请看这里:http://www.mnot.net/cache_docs/

要回答有关如何获取 30 天前的时间的问题,您可以使用非常方便的strtotime

$ts = gmdate("D, d M Y H:i:s", strtotime("30 days ago")) . " GMT";

【讨论】:

  • 时间怎么样..可以吗..我该如何处理?
  • 是否可以包含一个 if 语句来识别 304 响应。?它是一个包含单词的字符串:“304 Not Modified”
  • 大声笑..我返回了整个页面...我想知道我什么时候没有收到回复...请参阅上面的更新
  • 如何测试该语句以使其产生 304?也许减少时间?...奇怪,它不起作用,即使我现在把它放在 strtotime 中..
  • lol..with this link 我没有得到回应..如果我把它放在代码中:sexygreg.com/…
猜你喜欢
  • 2013-09-20
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 2011-07-02
  • 2020-11-08
相关资源
最近更新 更多