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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?