Tag Archive for csv

Import Export CSV routine for CakePHP

// Exports CSV
function exportCSV($where = NULL, $delimeter = ',')
{
$csv = '';
$this->recursive = -1;
$rows = $this->findAll($where);
foreach($rows as $row)
{
$row[$this->name] = str_replace('', '',
$row[$this->name]);
$row[$this->name] = str_replace($delimeter, '' .
$delimeter, $row[$this->name]);
$csv .= implode($delimeter, $row[$this->name]) . chr(13);
}

$csv = trim($csv);
return $csv;
}

// Imports CSV
function importCSV($csv, $delimeter = ',')
{
$keys = $this->getFieldNames();
$rows = array();
$csv = trim($csv);
$csv_rows = explode(chr(13), $csv);

foreach($csv_rows as $csv_row)
{
$csv_row = str_replace('', '', $csv_row);
$csv_row = str_replace('' . $delimeter, chr(26), $csv_row);
$row = explode($delimeter, $csv_row);
$row = str_replace(chr(26), $delimeter, $row);
$row = array_combine($keys, $row);
$rows = array_merge_recursive($rows,
array(array($this->name => $row)));
}

return $rows;
}

// Returns model fieldnames
function getFieldNames()
{

$names = array();
$fields = $this->_schema;

foreach($fields as $key => $value)
array_push($names, $key);

return $names;
}

source

reading CSV file with PHP

<?php
/*
your CSV should look like this:
data1;data2;data3;
data1;data2124;data3;
data1;data2564;data3;
data1;data2321;data3;
*/

$fileCont = join('', file('myCSV.csv'));
$fileArrN = explode("
", $fileCont);

$a = 0;
foreach ($fileArrN as $value){
$fileArrSemi[$a] = explode(";", $value);
$a++;
}

$output = null;
for($i = 0; $i < sizeof($fileArrSemi); $i++){
$output .= $fileArrSemi[$i][1]."<br />
";
}

echo $output;
?>

source

DISPLAY MANAGER commands used to IMPORT and EXPORT the Tab delimited (Excel and .CSV) files;

One of my favorite methods of exporting excel or .csv file is to use the �DEXPORT� command-line command. This certainly reduces the amount of typing required to export the SAS dataset. Another interesting point is DEXPORT command works fine in UNIX and PC.

contd...

source

CSV example for working with myIO

"""
Name : CSV.py
Author : Jason Spadaro
Description : Module to create comma seperated value objects
Copyright 2009
"""

from MyIO import *
import csv

class CSV(myIO):
"""
****************************************************
*Description : Makes comma seperated values file connections
****************************************************
"""

def make_connect(self):
"""
****************************************************
*Description : Opens the comma seperated values file
****************************************************
"""
self.file_name = self.connectDict["file_name"]
csv_reader = csv.reader(open(self.file_name))
for row in csv_reader:
self.data_rows.append(row)
self.csv_writer = csv.writer(open(self.file_name), "w")

def write_item(self, item):
"""
****************************************************
*Description : Writes to the comma seperated values file
****************************************************
""""
temp_list = []
for name, valitem in item.iteritems():
temp_list.append(item[name])
self.csv_writer.writerow(temp_list)

source

Generic superclass for dealing with io based tables

"""
Name : MyIO.py
Author : Jason Spadaro
Description : superclass for io-based classes and objects
Copyright 2009
"""

class myIO:
"""
********************************************
*Description : returns items from an external storage source, and
*   stores them again later.
*******************************************
"""

def __init__(self, connectDict):
"""
********************************************
*Description : Initializes the connection information, rows
*   of data.  Then it makes the connection based on the myIO
*   subclass.
*******************************************
"""

self.data_rows = []             #Empty array of items...
self.item_names = connectDict["item_names"]    #List of item attribute names
self.connectDict = connectDict  #connection information (filename, db name, etc)
self.make_connect()

def make_connect(self):
"""
********************************************
*Description : connection method
*******************************************
"""

pass

def getItem(self, i):
"""
********************************************
*Description : get's one item's worth of data.
*******************************************
"""
temp_dict = {}
for attribute, name in self.datarows[i], item_names:
temp_dict.update({name:attribute})
return temp_dict

def writeItem(self):
"""
********************************************
*Description : Writes one item worth of data
*******************************************
"""

pass

def close(self):
"""
********************************************
*Description : Cleans up io when it closes.
*******************************************
"""

pass

def dump_current_dict(self, item_dict):
"""
********************************************
*Description : Generically writes all items to io
********************************************
"""

for item_name, item in item_dict.iteritems():
print item_name + " is being written."
self.write_item(item)

def get_item_dict(self):
"""
********************************************
*Description : Getter for the aggregator
********************************************
"""

tempItemDict = itemDict()
for i in range(self.data_rows):
tempItem = getItem(i)
tempItemDict.add(tempItem)
return tempItemDict

source

CSV Parse

String.prototype.parseDelim = function(delim,columns){
var qo = false; // qo = quotes open
var rc = 0; // rc = row count
var cc = 0; // cc = column count
var i = 0; // i = seek index
var bi = 0; // bi = beginning index
var ln = this.length // ln = string length;
var data = [] // data = data output
var cp = this.replace(/
/g,"
"); // copy = this string, with normalized newlines
var v = "";// process value
// re-usable fill function
var fill = function(value){
v = value.replace(/^s*|s*$/g,"").replace(/""/g,'"');
if(v[0] == '"' && v[v.length-1] == '"'){
v = v.substring(1,v.length-1);
}
if(data.length < (rc+1)) data[data.length] = [];
data[data.length-1][cc] = v;
cc +=1;
if(cc == columns){
rc += 1;
cc = 0;
}
bi = bi+value.length;
};
if(ln > 1){
while(i<ln){
// switch status of quotes open with each double quote
if(cp[i] == '"'){
qo = !qo;
// store value if a delimiter or newline is hit and quotes aren't open
} else if(cp[i] == delim && qo === false){
fill(cp.substring(bi,i));
} else if(cp[i] == "
" && qo === false){
fill(cp.substring(bi,i));
}
i++;
}
// store final value
fill(cp.substring(bi,i));
return data;
} else {
return [this];
}
}

source

How to Create a .csv file of a SAS dataset

www.studysas.blogspot.com

source

CSV formatting

<?php

/**
* Mailing List Script
* Copyright 2009 - <a href="http://www.pgmr.co.uk" >www.pgmr.co.uk</a> - <a href="mailto:contact@pgmr.co.uk">contact@pgmr.co.uk</a>
*/

function showForm() {
echo '
<form method="post" action="">
Email Address: <input type="text" name="email"> <br />
<input type="submit" value="Submit" name="submit">
</form>
';
}

if(empty($_POST['submit']) === false) {
$email = htmlentities(strip_tags($_POST['email']));

$logname = 'email.txt';
$logcontents = file_get_contents($logname);

if(strpos($logcontents,$email)) {
die('You are already subscribed.');
} else {
$filecontents = $email.',';
$fileopen = fopen($logname,'a+');
$filewrite = fwrite($fileopen,$filecontents);
$fileclose = fclose($fileopen);
if(!$fileopen or !$filewrite or !$fileclose) {
die('Error occured');
} else {
echo 'Your email has been added.';
}
}
} else {
showForm();
}

?>

source

Hypovereinsbank CSV crawler – online banking statements downloader from Hypovereinsbank Munich, Germany using PHP & CURL

<?php

setlocale(LC_TIME, "de_DE");
global $location; #keep track of location/redirects
global $cookiearr; #store cookies here
global $ch;

global $Betrag;
$inputUsername = 'XXXXXXXXXX'; # place here the Direct banking number
$inputPassword = 'XXXXXX';   # place here your pass

$outputCSV = '/tmp/Umsatzliste.csv';

require_once 'func/hypo_functions.php';

$step = 0;
logF("fetch start");

define ('RND_LOW', 2);
define ('RND_HIGH', 4);
#define ('RND_LOW', 5);
#define ('RND_HIGH', 10);

$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Host: my.hypovereinsbank.de';
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
$headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$headers[] = 'Keep-Alive: 300';

###################################################################

logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);

$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp';
$cookieFilenameLogin="/tmp/hypo_login.cookie";
$cookieFilenameAuth="/tmp/hypo_auth.cookie";

# first HTTP session : retrieve tr_sid, setcookie etc
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);

$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
#		$javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
###################################################################

$Url .= '&tr_sid='. $tr_sid;
$postFields = array (
#	'directBankingLoginForm:viewInitialized' => 'true',
'username' => $inputUsername,
'px2' => $inputPassword,
'secP' => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
'directBankingLoginForm:loginPanel:loginCommand' => 'Anmelden',
'directBankingLoginForm:_idcl' => '',
'directBankingLoginForm:_link_hidden_' => '',
'directBankingLoginForm_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
$postUrl = http_build_query_wrong($postFields);

# second HTTP session : effective login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameAuth);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################

###################################################################

logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);

$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp';
$cookieFilenameLogin="/tmp/hypo_login.cookie";
$cookieFilenameAuth="/tmp/hypo_auth.cookie";

# first HTTP session : retrieve tr_sid, setcookie etc
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);

$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
#		$javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
###################################################################

$Url .= '&tr_sid='. $tr_sid;
$postFields = array (
#	'directBankingLoginForm:viewInitialized' => 'true',
'username' => $inputUsername,
'px2' => $inputPassword,
'secP' => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
'directBankingLoginForm:loginPanel:loginCommand' => 'Anmelden',
'directBankingLoginForm:_idcl' => '',
'directBankingLoginForm:_link_hidden_' => '',
'directBankingLoginForm_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
$postUrl = http_build_query_urlencode($postFields);

# second HTTP session : effective login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameAuth);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);

$lines = split ("
", $Html);
foreach ($lines as $line) {
if (strpos($line, 'mystartpage_finanzstatus')!== false) {
$pieces = explode(''', $line);
$Var_idcl = $pieces[3];
}
}
if (!$Var_idcl) {
print "ERROR: Cannot fetch idcl";
#	exit;
}
logF($step ."th idcl: $Var_idcl");
$cookieStr = '';
print_r($cookiearr);
foreach ($cookiearr as $cookieName => $cookieVal) {
$cookieStr.=$cookieName.'='.$cookieVal.'; ';
}
$cookieStr = substr($cookieStr, 0, -2);
###################################################################

###################################################################
# go to account
$Url='https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIE,  $cookieStr);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
###################################################################

$lines = split ("
", $Html);
$liveStart = false;
foreach ($lines as $line) {
if (strpos($line, 'Aktueller Kontosaldo')!== false) {
$liveStart = true;
}
if (strpos($line, '</fieldset>')!== false) {
$liveStart = false;
}
if ($liveStart && trim($line) == trim(strip_tags($line)) && strpos($line, 'EUR')!== false) {
$Betrag[0] = trim(str_replace('EUR', '', $line));
}
if ($liveStart && strpos($line, 'negbal')!==false && strpos($line, 'EUR')!== false) {
$Betrag[0] = trim(str_replace('EUR', '', strip_tags($line)));
}

if (strpos($line, 'Kontostand am')!== false) {
$linePart = strip_Tags(substr($line, strpos($line, 'Kontostand am')));
$lineParts = explode(date('Y'), $linePart);
$lineParts2 = explode(' ', trim(str_replace('EUR', '', $lineParts[1])));
if ($lineParts2[1]) {
$Betrag[1] = $lineParts2[1];
} else {
$Betrag[1] = trim(str_replace('EUR', '', $lineParts[1]));
}
}
}
logF("Kontostand live ... " . $Betrag[0] ." EUR");
logF("Kontostand old ...  " . $Betrag[1] ." EUR");
if (!$Betrag) {
die (print 'no Betrag in step: '. $step);
}

$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
#		$javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");

#var_dump($Betrag);
###################################################################
$Url = 'https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';
$postFields = array (
'accountManagement:dayFrom' => '1',
'accountManagement:monthFrom' => strftime("%B %Y", time() - 31*24*3600),
'accountManagement:dayTo' => date('d'),
'accountManagement:monthTo' => strftime("%B %Y"),
'accountManagement:numberOfTurnovers' => '9999',
'accountManagement:refresh' => 'Anzeigen',
#	'accountManagement:buttonNavigation:j_id_id142' => 'Download Kontoums�¤tze ',
'accountManagement:_link_hidden_' => '',
'accountManagement:_idcl' => '',
'accountManagement_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);

$postUrl = http_build_query_urlencode($postFields);

# post-login steps
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIE,  $cookieStr);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'https://my.hypovereinsbank.de/portal?view=/banking/startpage.jsp');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);

$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
#		$javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");

logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################

###################################################################
$Url = 'https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';
$postFields = array (
'accountManagement:dayFrom' => max(date('d')-7, 1),
'accountManagement:monthFrom' => strftime("%B %Y", time() - 31*24*3600),
##	'accountManagement:monthFrom' => strftime("%B %Y"),
'accountManagement:dayTo' => date('d'),
'accountManagement:monthTo' => strftime("%B %Y"),
'accountManagement:numberOfTurnovers' => '9999',
##	'accountManagement:numberOfTurnovers' => '20',
#	'accountManagement:refresh' => 'Anzeigen',
'accountManagement:buttonNavigation:j_id_id147' => 'Download Kontoums'. substr(strftime("%B", strtotime('2001-03-01 00:00:00')), 1, 1) .'tze ',
'accountManagement:_link_hidden_' => '',
'accountManagement:_idcl' => '',
'accountManagement_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
print_R($postFields);
$postUrl = http_build_query_urlencode($postFields);
print_R($postUrl);

# post-login steps
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIE,  $cookieStr);
#curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
#curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
#curl_setopt($ch, CURLOPT_REFERER, 'https://my.hypovereinsbank.de/portal?view=/banking/startpage.jsp');
curl_setopt($ch, CURLOPT_REFERER, $Url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
#curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 2);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################

unlink($outputCSV);
copy("$step.html", $outputCSV);

###################################################################

###################################################################
# party over, logout
$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/logout.jsp';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIE,  $cookieStr);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);

$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
###################################################################

###################################################################
function http_build_query_urlencode($postFields)
{
$postUrl = "";
foreach ($postFields as $fName => $fValue) {
$postUrl.=urlencode($fName).'='.urlencode($fValue)."&";
#		$postUrl.=($fName).'='.rawurlencode($fValue)."&";
}
return substr($postUrl, 0, -1);
}

function http_build_query_wrong($postFields)
{
$postUrl = "";
foreach ($postFields as $fName => $fValue) {
$postUrl.=urlencode($fName).'='.utf8_encode($fValue)."&";
#		$postUrl.=($fName).'='.rawurlencode($fValue)."&";
}
return substr($postUrl, 0, -1)."
";
}

function read_header($ch, $string)
{
global $location; #keep track of location/redirects
global $cookiearr; #store cookies here
global $ch;
# ^overrides the function param $ch
# this is okay because we need to
# update the global $ch with
# new cookies

$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{ #keep track of last redirect
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{ #get the cookie
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));
$cookiearr[$cookiename] = trim(implode('=', $cookie));
}
$cookie = "";
if(trim($string) == "")
{  #execute only at end of header
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

return $length;
}

?>

source

Export data to CSV

<?php
$db = mysql_connect('localhost', 'username', 'password'); // Connect to the database
$link = mysql_select_db('database name', $db); // Select the database name

function parseCSVComments($comments) {
$comments = str_replace('"', '""', $comments); // First off escape all " and make them ""
if(eregi(",", $comments) or eregi("
", $comments)) { // Check if I have any commas or new lines
return '"'.$comments.'"'; // If I have new lines or commas escape them
} else {
return $comments; // If no new lines or commas just return the value
}
}

$sql = mysql_query("SELECT * FROM tableName"); // Start our query of the database
$numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching

if($numberFields) { // Check if we need to output anything
for($i=0; $i<$numberFields; $i++) {
$head[] = mysql_field_name($sql, $i); // Create the headers for each column, this is the field name in the database
}
$headers = join(',', $head)."
"; // Make our first row in the CSV
$data = "";
while($info = mysql_fetch_object($sql)) {
foreach($head as $fieldName) { // Loop through the array of headers as we fetch the data
$row[] = parseCSVComments($info->$fieldName);
} // End loop
$data .= join(',', $row)."
"; // Create a new row of data and append it to the last row
$row = ''; // Clear the contents of the $row variable to start a new row
}
// Start our output of the CSV
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=log.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $headers.$data;
} else {
// Nothing needed to be output. Put an error message here or something.
echo 'No data available for this CSV.';
}
?>

source