使用PHP Curl从Google电子表格中获取工作表和ListFeed

时间:2014-05-08 06:48:58

标签: php excel curl google-apps-script google-spreadsheet-api

我正在使用这个面向对象的php类将数据发送到Google Docs电子表格 -

http://code.google.com/p/php-form-builder-class/source/browse/trunk/includes/class.spreadsheet.php?spec=svn384&r=384

public function get()
{
    if(!empty($this->token)) {
        $url = $this->getPostUrl();
        $headers = array(
            "Authorization: GoogleLogin auth=" . $this->token,
            "GData-Version: 3.0",
            );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($curl);   
        $response = simplexml_load_string($response);
        return $response;
    }
}

我从另一个文件调用此函数。像:

$doc = new spreadsheet();
$doc->authenticate("email", "pass");
$doc->setSpreadsheet("xxxxx");
$doc->setWorksheet("xxx");//here is static name of worksheet
var_dump($doc->get());

它的工作。

问题:我想获取电子表格的所有工作表 listfeed ,而不是静态传递。因为我的电子表格有很多工作表。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

要获取工作表列表,请使用以下网址:

"https://spreadsheets.google.com/feeds/worksheets/" + key + "/private/full"

要获取电子表格列表,请使用以下网址:

"https://spreadsheets.google.com/feeds/spreadsheets/private/full"

其他一些人:

public URL getSpreadsheetsUrl() throws MalformedURLException {
    return new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");
}

public URL getSpreadsheetUrl(String key) throws MalformedURLException {
    return new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full/"
                    + key);
}

public URL getWorksheetsUrl(String key) throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
            + "/private/full");
}

public URL getWorksheetUrl(String key, String worksheetId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
            + "/private/full/" + worksheetId);
}

public URL getCellsUrl(String key, String worksheetId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/cells/" + key
            + "/" + worksheetId + "/private/full");
}

public URL getCellUrl(String key, String worksheetId, String cellId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/cells/" + key
            + "/" + worksheetId + "/private/full/" + cellId);
}


public URL getTableUrl(String key, String worksheetId, String rowNumber)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/" + key
            + "/records/" + rowNumber);
}

答案 1 :(得分:0)

我找到了解决方案。我创建了一个函数,其中包含所有工作表的返回数组,其中 ID

功能定义:

private function getWorkSheet() {

        $sheetArray = array();
        $url = "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=" . urlencode($this->spreadsheet);

        //echo "$url";exit;
        $headers = array(
                "Authorization: GoogleLogin auth=" . $this->token,
                "GData-Version: 3.0"
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);



        if($status == 200) {
                $spreadsheetXml = simplexml_load_string($response);
                //print_r($spreadsheetXml);
                if($spreadsheetXml->entry) {
                        $this->spreadsheetid = basename(trim($spreadsheetXml->entry[0]->id));
                        $url = "https://spreadsheets.google.com/feeds/worksheets/" . $this->spreadsheetid . "/private/full";
                        if(!empty($this->worksheet))
                                $url .= "?title=" . $this->worksheet;

                        curl_setopt($curl, CURLOPT_URL, $url);
                        $response = curl_exec($curl);

                        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
                        if($status == 200) {
                                $worksheetXml = simplexml_load_string($response);
                                foreach($worksheetXml->entry as $entry)
                                {
                                    $temp = array('title'=>trim($entry->title),'id'=>basename(trim($entry->id)));
                                    array_push($sheetArray,$temp);
                                }
                                if($worksheetXml->entry)
                                        $this->worksheetid = basename(trim($worksheetXml->entry[0]->id));
                        }
                }
        }
        curl_close($curl);

        return ($sheetArray);
}

public function test()
{
    $sheet = $this->getWorkSheet();//calling my function here
    print_r($sheet);
}

如果要获取工作表。您只需将此功能放在Class File

中即可