0

I've developed a WordPress plugin called 'CSV-to-SortTable' that imports CSV file data, parses it into an indexed array, and outputs it as an table using Stuart Langridge's sorttable.js.

The plugin works well, but I want to add an option for users to import data from a Google Spreadsheet. Most importantly, I want to re-use the existing code as much as possible to avoid any redundancy.

I've spent hours sifting through Google's documentation and countless forums, and I think I'm close...but I keep getting a 'Moved Temporarily' message which, based on my research, indicates that the URL I'm using gets redirected to an authorization page.

Here is what I've got so far:

PHP Code:

/* Source = Google Spreadsheet */
if( $atts[gkey] <> '' ) {
    $atts[csv] = 'https://spreadsheets.google.com/pub?key=' . $atts[gkey] . '&output=csv&ndplr=1';
}

/* Source = CSV file or Google Spreadsheet */
if( $atts[csv] <> '' ) {
    $session = curl_init();
    curl_setopt( $session, CURLOPT_HEADER, 0 );
    curl_setopt( $session, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $session, CURLOPT_URL, $atts[csv] );
    $csv_data = curl_exec( $session ) or die( 'CURL ERROR: '.curl_error( $session ) );
    curl_close( $session );

    /* Call function to parse .CSV data string into an indexed array. */
    $csv_data_array = $this->parse_csv_data( $csv_data );

    /* Call function to render the table. */
    print_r( $this->table_data );

}

Explanation: The value of $atts[gkey] is a user-defined shortcode parameter. If it is set to the key of a Google Spreadsheet, I want the plugin to define $atts[csv] as a URL that will output the contents of the Google Spreadsheet as a CSV file. (Normally, the user would define $atts[csv] as the URL of a .CSV file.)

Testing Site URL: http://sandbox.mynewsitepreview.com/test/wp-sorttable/

Public Google Spreadsheet URL: https://docs.google.com/spreadsheet/ccc?key=0Aj-ZlsTpsY_wdEhIdTdhYzlmTmdEMXhjVEJaWERtUFE

CSV File URL: http://mynewsitepreview.com/plugins/sample.csv

On this testing site, I am first using the shortcode with the 'csv' parameter set to the URL of a CSV file, then using the shortcode again but with the 'gkey' parameter set to the KEY of the Google Spreadsheet. The two outputs should match, but clearly they do not.

Credit: I will gladly give contribution credits to the person who provides the most elegant solution.

Bonus Points if you can find a simple way to access a password-protected Google Spreadsheet, using the shortcode parameters: $atts[guser] and $atts[pw].

2
  • Just found this thread, which may hold the key... google.com/support/forum/p/apps-apis/… Mar 27, 2012 at 16:45
  • Accessing password-protected Google Spreadsheets requires a greater understanding of OAuth than I can muster at this time. If someone wants to contribute that piece of the plugin, that would be awesome. But for now, I'd just be happy getting data from public spreadsheets without having to deal with authorizations and redirects. Anybody? Mar 27, 2012 at 18:01

2 Answers 2

0

If you're getting a "Moved Temporarily" error, you probably should tell CURL to follow redirects (301) :

curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );

You will see if the data you're getting is an auth page or a CSV. There is a well documented API for Google Spreadsheets, you probably should look into it.

3
  • Good suggestion, but unfortunately that did not work. It's still returning the "Moved Temporarily" error. Mar 27, 2012 at 16:11
  • Another good idea, thank you. That function actually returned the HTML for the Google docs login page, complete with images. I can't seem to figure out how to just bypass authentication altogether. It should be possible, since the spreadsheet is public... Mar 28, 2012 at 20:28
  • 1
    Then your solution can't be "quick and dirty", you will have to use the Google API with OAuth.
    – Ugo Méda
    Mar 29, 2012 at 7:49
0

CURLOPT_FOLLLOWLOCATION is the option to use if you want curl to follow HTTP redirects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.