Shoora
10/3/2018 - 4:10 PM

google spreadsheet to CSV

google spreadsheet to CSV

<?php

function get_details($key, $email) {

    $csv = 'https://spreadsheets.google.com/pub?key=' . $key . '&output=csv&ndplr=1';

    $session = curl_init();
    curl_setopt( $session, CURLOPT_HEADER, 0 );
    curl_setopt( $session, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $session, CURLOPT_URL, $csv );
    curl_setopt( $session, CURLOPT_MAXREDIRS, 10 );
    curl_setopt( $session, CURLOPT_MAXREDIRS, TRUE );

    $csv_data = curl_exec( $session ) or die( 'CURL ERROR: '.curl_error( $session ) );
    curl_close( $session );

    $data = str_getcsv($csv_data, $eol="\n");
    for( $i=0; $i< count($data); $i++ ) {
        if ( strpos($data[$i], $email) ) {
            return str_getcsv($data[$i]);
        }
    }

    return $arrayName = array();
}

$key = "0Ai-CG4NMV2RgdDZ2VFFka2ZRdzFPUFhqU1h1dUstZ2c";
$details = get_details($key, "bob@somewhere.com");
print("Name: " . $details[0] . "\n");
print("Age: " . $details[1]) . "\n";
print("Email: " . $details[2] . "\n");

?>