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;
}
?>
Try this function:
function containsDate($url){
preg_match("/\/EQ\d{6}_CSV\.ZIP/", $url, $matches);
return !empty($matches[0]);
}
You can use preg_match:
preg_match('/EQ([0-9]+)_/'), $url, $matches);
$date = $matches[1];
To run a cron to do that daily you simply need to type in crontab -e
on the shell.
Here is a sample cronline for a daily run at 4am:
0 4 * * * php /home/user/website/bin/your_script.php
# leave an empty line on the bottom of the crontab
Find help here: http://www.thegeekstuff.com/2011/07/php-cron-job/