Jump to content

Concatenate wget variables in url using shell


bschultz

Recommended Posts

I'm trying to get a php script working to download the latest CNN news podcast each hour.  CNN names the file based on the year, month, day, and time.

 

Here's what I'm trying:

 

<?php
$year = date('Y');
$month = date('m');
$day = date('d');
$now = date('Y-m-d-h');
$hour = date('gA');
$hourplus1 = ($hour + 1);
$hourminus1 = ($hour - 1);
$ampm = date('A');

$url = "http://podcasts.cnn.net/cnn/services/podcasting/newscast/" . "audio/" . "$year" . "/" . "$month" . "/" . "$day" . "/CNN-News-" . "$month" . "-" . "$day" . "-" . "$year" . "-" . "$hourplus1" . "$ampm" . ".mp3";

echo $url;
echo system('wget "$url"');
?>

 

When running from the shell, I get an "http://: invalid hostname" error.  The echo of $url looks right...but it won't run from shell.

 

Any ideas?

 

Here you go. I fixed up your hour calculation too and the years :)

 

<?php

//date_default_timezone_set('US/Eastern');

$fullyear = date('Y');
$year = date('y');
$month = date('m');
$day = date('d');
$now = date('Y-m-d-h');
$hour = date('g');
$hourplus1 = date('g', strtotime("Now +1 hour"));
$hourminus1 = date('g', strtotime("Now -1 hour"));
$ampm = date('A');

$url = "http://podcasts.cnn.net/cnn/services/podcasting/newscast/" . "audio/" . "$fullyear" . "/" . "$month" . "/" . "$day" . "/CNN-News-" . "$month" . "-" . "$day" . "-" . "$year" . "-" . "$hourminus1" . "$ampm" . ".mp3";

echo system('wget '.escapeshellcmd($url));
?>

 

Here's a shorter way of doing this:

$path = date('Y/m/d/\C\N\N-\N\e\w\s-m-d-y-gA', strtotime("Now -1 hour"));
$url = "http://podcasts.cnn.net/cnn/services/podcasting/newscast/" . "audio/" . $path . ".mp3";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.