title: How can I get the contents of a Google Spreadsheet as CSV data in the simplest way imaginable?
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is what I've got so far:</p>
  
  <p><strong>PHP Code:</strong></p>
  
  <pre><code>/* Source = Google Spreadsheet */
  if( $atts[gkey] &lt;&gt; '' ) {
      $atts[csv] = 'https://spreadsheets.google.com/pub?key=' . $atts[gkey] . '&amp;output=csv&amp;ndplr=1';
  }
  
  /* Source = CSV file or Google Spreadsheet */
  if( $atts[csv] &lt;&gt; '' ) {
      $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-&gt;parse_csv_data( $csv_data );
  
      /* Call function to render the table. */
      print_r( $this-&gt;table_data );
  
  }
  </code></pre>
  
  <p><strong>Explanation:</strong> 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.)</p>
  
  <p><strong>Testing Site URL:</strong> <a href="http://sandbox.mynewsitepreview.com/test/wp-sorttable/" rel="nofollow">http://sandbox.mynewsitepreview.com/test/wp-sorttable/</a></p>
  
  <p><strong>Public Google Spreadsheet URL:</strong> <a href="https://docs.google.com/spreadsheet/ccc?key=0Aj-ZlsTpsY_wdEhIdTdhYzlmTmdEMXhjVEJaWERtUFE" rel="nofollow">https://docs.google.com/spreadsheet/ccc?key=0Aj-ZlsTpsY_wdEhIdTdhYzlmTmdEMXhjVEJaWERtUFE</a></p>
  
  <p><strong>CSV File URL:</strong> <a href="http://mynewsitepreview.com/plugins/sample.csv" rel="nofollow">http://mynewsitepreview.com/plugins/sample.csv</a></p>
  
  <p>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.</p>
  
  <p><strong>Credit:</strong> I will gladly give contribution credits to the person who provides the most elegant solution.</p>
  
  <p><strong>Bonus Points</strong> if you can find a simple way to access a password-protected Google Spreadsheet, using the shortcode parameters: $atts[guser] and $atts[pw].</p>