doy2255 2016-05-12 06:30
浏览 42
已采纳

如何检查URL是否包含PHP中的日期

I want to build an automated function to automatically upload reports from a URL at end of the day if found on that URL. My question is the URL keeps changing daily with a date. How can I obtain whether the URL has a date? Here I am pasting the url from which i need to extract data

www.bseindia.com/download/BhavCopy/Equity/EQ140316_CSV.ZIP

So the string after "Equity/" needs to be validated as it contains the date. How to build a function in php fro that.. It will be a great help. Thanks in advance

here is the code that I am following,

<?php

$url = 'http://www.bseindia.com/download/BhavCopy/Equity/EQ140316_CSV.ZIP';
if ( preg_match("/\/EQ\d{6}_CSV\.ZIP/", $url, $matches)) {
$path = '/upload/';

$headers = getHeaders($url);

if ($headers['http_code'] === 200 and $headers['download_content_length'] < 1024*1024) {
  if (download($url, $path)){
    echo 'Download complete!'; 
  }
}
}


/**
 * Get Headers function
 * @param str #url
 * @return array
 */
function getHeaders($url)
{
  $ch = curl_init($url);
  curl_setopt( $ch, CURLOPT_NOBODY, true );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  curl_setopt( $ch, CURLOPT_HEADER, false );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
  curl_exec( $ch );
  $headers = curl_getinfo( $ch );
  curl_close( $ch );

  return $headers;

}

 /**
 * Download
 * @param str $url, $path
 * @return bool || void
 */
function download($url, $path)
{
  # open file to write
  $fp = fopen ($path, 'w+');
  # start curl
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  # set return transfer to false
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  # increase timeout to download big file
  curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  # write data to local file
  curl_setopt( $ch, CURLOPT_FILE, $fp );
  # execute curl
  curl_exec( $ch );
  # close curl
  curl_close( $ch );
  # close local file
  fclose( $fp );

  if (filesize($path) > 0) 
      return true;
}


?>
  • 写回答

2条回答 默认 最新

  • douxiongye5779 2016-05-12 06:55
    关注

    Try this function:

    function containsDate($url){
       preg_match("/\/EQ\d{6}_CSV\.ZIP/", $url, $matches);
       return !empty($matches[0]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 COMSOL频域研究,要加法向加速度的话,加速度随时间变化该如何设置?
  • ¥15 无刷电机要通过什么才能做各种动作
  • ¥15 VS2010通过OnCtlColor函数改变radio控件字体的颜色为白色,但是程序运行后发现不起作用,怎么办?
  • ¥15 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 Vue3重构Nuxt3,出现客户端数据和服务端数据不一致的情况
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿