Jump to content

Recommended Posts

Posted

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="tweets.csv"');

$tag = urlencode($_GET['tag']);

$max_pages = $_GET['maxpages'];
if ($max_pages > 50)
{
die("No way am i doing more than 50 pages. Sorry mate.");
}

echo 'name,date,uri,content' . "\r\n";
for($page = 1; $page <= $max_pages; $page++) 
{
	$url = "http://search.twitter.com/search.atom?&q={$tag}&rpp=100&page={$page}";
	$xmlstr = file_get_contents($url);

	$xml = new SimpleXMLElement($xmlstr);
		foreach ($xml->entry as $entry) 
		{

$twitterdate = $entry->updated;
$middle = " ";
$half = (int) ( (strlen($twitterdate)) ); // cast to int incase str length is odd
$left = substr($twitterdate, 0, $half);
$right = substr($twitterdate, $half);
$unixdate = strtotime($left);
$correctdate = date("d-m-Y H:i", $unixdate);


		echo $entry->author->name . "," . $correctdate . "," . $entry->author->uri . ",\"" . strip_tags($entry->content) . "\" \r\n";
		}
}

 

This currently saves the RSS feed from the Twitter search to a .CSV file.

 

I'd like to not only allow the file to be downloaded but also get it saved to the server. How can I generate the file and once generated post it to the server?

 

 

*****Please excuse my horrible date functions!!!!!!

Posted

Could anyone suggest an alternative?

 

My RSS to CSV file below - When the CSV is downloaded I want to save a copy of the file generated in the background to my server?

 

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="tweets.csv"');

$tag = urlencode($_GET['tag']);

$max_pages = $_GET['maxpages'];
if ($max_pages > 50)
{
die("No way am i doing more than 50 pages. Sorry mate.");
}

echo 'name,date,uri,content' . "\r\n";
for($page = 1; $page <= $max_pages; $page++) 
{
	$url = "http://search.twitter.com/search.atom?&q={$tag}&rpp=100&page={$page}";
	$xmlstr = file_get_contents($url);

	$xml = new SimpleXMLElement($xmlstr);
		foreach ($xml->entry as $entry) 
		{

$twitterdate = $entry->updated;
$middle = " ";
$half = (int) ( (strlen($twitterdate)) ); // cast to int incase str length is odd
$left = substr($twitterdate, 0, $half);
$right = substr($twitterdate, $half);
$unixdate = strtotime($left);
$correctdate = date("d-m-Y H:i", $unixdate);


		echo $entry->author->name . "," . $correctdate . "," . $entry->author->uri . ",\"" . strip_tags($entry->content) . "\" \r\n";
		}
}


?>

Posted

fopen to create the file and fwrite to put the contents into the created file.

 

so

 

 

$hndl = fopen('/path/to/myfile.csv', 'x');

 

 

//SOME FOR LOOP, WRITE FORMATTED TWITTER DATA eg, x,y,z,1,2,3

 

fwrite($hndl, $twitterstringvar);

 

//END FOR LOOP

 

fclose($hndl);

Posted
Just make sure the path to your serverside file does not contain *any* elements of user input (if it must, sanitise the hell out of them, but it is still a bad idea).

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...