如何在没有 Zend Gdata 库的情况下使用 PHP 将数据保存在非公开的谷歌电子表格中?

发布于 2024-08-06 18:10:09 字数 229 浏览 10 评论 0原文

如何在不使用 Zend Gdata lib 的情况下在 PHP 中将数据保存在非公开的 Google 电子表格中? 我无法使用 Zend libs,正如 google 教程中所示,因为我运行脚本的 php 服务器是 php v. 5.0.4。 我尝试使用 cUrl 找到解决方案,但当 doc 不公开时,我不能忽略身份验证问题。 我该怎么做呢?如果有人尝试过,请分享解决方案。

How can I save data in unpublic google spreadsheet in PHP without using Zend Gdata lib?
I can't use Zend libs, as it's shown on google tutorials, because the php server on which I'm running the script is php v. 5.0.4.
I tried to find a solution using cUrl, but I can't omit the problem with authentication when doc is unpublic.
How can I do it? If anyone has attempted it, please share the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-08-13 18:10:09

我已经找到了使用curl 并通过为谷歌电子表格创建表单的解决方案。
对于准备好的电子表格,您必须创建一个表单,不带选项:需要登录才能查看此表单自动收集受访者的用户名
然后使用 fe firebug 检查表单 post uri 和 post 数据,并将其用于以下脚本:

#prepare post data
$fields = array('backupCache' => '',
            'entry.0.single'=>urlencode($data['name']),
            'entry.1.single'=>urlencode($data['surname']),
            'pageNumber'=>urlencode(0),   
            'submit'=>'Submit');
$fields_string = '';

foreach($fields as $key=>$value) {
  $fields_string .= $key.'='.$value.'&';
}

rtrim($fields_string,"& ");
$fields_string = substr($fields_string, 0, strlen($fields_string)-1);

$ch = curl_init();
#set curl_setopt for your preferences
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);    

curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

#set proper form uri
curl_setopt($ch, CURLOPT_URL, $formUri);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

$res = curl_exec($ch);
curl_close($ch);

可能不是最完美的解决方案,但它有效。 :)

I have found out solution using curl and by creating a form for the google's spreadsheet.
For prepared spreadsheet you have to create a form, without options: Require sign-in to view this form and Automatically collect respondent's username.
Then check, using f.e. firebug, the form post uri and post data and use it to following script:

#prepare post data
$fields = array('backupCache' => '',
            'entry.0.single'=>urlencode($data['name']),
            'entry.1.single'=>urlencode($data['surname']),
            'pageNumber'=>urlencode(0),   
            'submit'=>'Submit');
$fields_string = '';

foreach($fields as $key=>$value) {
  $fields_string .= $key.'='.$value.'&';
}

rtrim($fields_string,"& ");
$fields_string = substr($fields_string, 0, strlen($fields_string)-1);

$ch = curl_init();
#set curl_setopt for your preferences
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);    

curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

#set proper form uri
curl_setopt($ch, CURLOPT_URL, $formUri);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

$res = curl_exec($ch);
curl_close($ch);

May not be the most perfect solution but it works. :)

装纯掩盖桑 2024-08-13 18:10:09

这是一个面向对象的 php 类,用于将数据发送到 Google 文档电子表格 - http://code.google.com/p/php-form-b​​uilder-class/source/browse/trunk/includes/class。 Spreadsheet.php?spec=svn384&r=384

使用 cUrl 代替 Zend 的 GData 库。您可以在下面找到示例实现。请记住将测试设置(“my_google_email”、“my_google_password”等)替换为您的具体信息。

$doc = new spreadsheet();
$doc->authenticate("my_google_email", "my_google_password");
$doc->setSpreadsheet("my_spreadsheet_title");
$doc->setWorksheet("my_worksheet_title");
$my_data = array("First Name" => "John", "Last Name" => "Doe");
$doc->add($my_data);

传递给 add 方法的关联数组的键需要与您用来收集数据的电子表格的列标题匹配。

Here is an object-oriented php class for sending data to a Google Docs Spreadsheet - http://code.google.com/p/php-form-builder-class/source/browse/trunk/includes/class.spreadsheet.php?spec=svn384&r=384

cUrl is used instead of Zend's GData lib. You can find a sample implementation below. Remember to replace the test settings ("my_google_email", "my_google_password", etc) with your specific information.

$doc = new spreadsheet();
$doc->authenticate("my_google_email", "my_google_password");
$doc->setSpreadsheet("my_spreadsheet_title");
$doc->setWorksheet("my_worksheet_title");
$my_data = array("First Name" => "John", "Last Name" => "Doe");
$doc->add($my_data);

The keys of the associative array passed to the add method need to match the column headers of the spreadsheets you're using to collect the data.

九局 2024-08-13 18:10:09

您可以使用普通 HTTP 请求访问 GData API;图书馆只是让这对你来说更容易。基本上,您最终只是重写了您想要使用的库的部分。

请参阅协议文档

You can access the GData APIs using ordinary HTTP requests; a library just makes this easier for you. You basically just end up rewriting the bits of the library you want to use.

See the protocol documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文