gpt4 book ai didi

php - 从昨天开始,即 26/05/2015,网站表单数据停止保存到 Google 电子表格中

转载 作者:可可西里 更新时间:2023-10-31 23:30:06 25 4
gpt4 key购买 nike

我创建了一个应用程序,我的网站表单数据将存储在一个谷歌电子表格中。直到昨天,即 2015 年 5 月 26 日,它都运行良好。但是从今天开始,即 27/05/2015 它突然停止工作了。谷歌电子表格中没有任何值(value)。

我已经将下面提到的类用于电子表格应用程序

电子表格.php:

<?php

class spreadsheet {
private $token;
private $spreadsheet;
private $worksheet;
private $spreadsheetid;
private $worksheetid;

public function __construct() {
}

public function authenticate($username, $password) {
$url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => $username,
"Passwd" => $password,
"service" => "wise",
"source" => "pfbc"
);
$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_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if($status == 200) {
if(stripos($response, "auth=") !== false) {
preg_match("/auth=([a-z0-9_\-]+)/i", $response, $matches);
$this->token = $matches[1];
}
}
}

public function setSpreadsheet($title) {
$this->spreadsheet = $title;
}

public function setWorksheet($title) {
$this->worksheet = $title;
}

public function add($data) {
if(!empty($this->token)) {
$url = $this->getPostUrl();
if(!empty($url)) {
$headers = array(
"Content-Type: application/atom+xml",
"Authorization: GoogleLogin auth=" . $this->token,
"GData-Version: 3.0"
);

$columnIDs = $this->getColumnIDs();
if($columnIDs) {
$fields = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
foreach($data as $key => $value) {
$key = $this->formatColumnID($key);
if(in_array($key, $columnIDs))
$fields .= "<gsx:$key><![CDATA[$value]]></gsx:$key>";
}
$fields .= '</entry>';

$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);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
}
}
}
}

private function getColumnIDs() {
$url = "https://spreadsheets.google.com/feeds/cells/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full?max-row=1";
$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);
curl_close($curl);

if($status == 200) {
$columnIDs = array();
$xml = simplexml_load_string($response);
if($xml->entry) {
$columnSize = sizeof($xml->entry);
for($c = 0; $c < $columnSize; ++$c)
$columnIDs[] = $this->formatColumnID($xml->entry[$c]->content);
}
return $columnIDs;
}

return "";
}

private function getPostUrl() {
$url = "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=" . urlencode($this->spreadsheet);
$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);
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);
if($worksheetXml->entry)
$this->worksheetid = basename(trim($worksheetXml->entry[0]->id));
}
}
}
curl_close($curl);
if(!empty($this->spreadsheetid) && !empty($this->worksheetid))
return "https://spreadsheets.google.com/feeds/list/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full";

return "";
}

private function formatColumnID($val) {
return preg_replace("/[^a-zA-Z0-9.-]/", "", strtolower($val));
}
}

?>

并按以下方式使用它:

包括“电子表格.php”;

$doc = new spreadsheet();
$doc->authenticate("example@example.com", "example");
$doc->setSpreadsheet("Tester");
$doc->setWorksheet("Sheet1");
$my_data = array("First Name" => "John", "Last Name" => "Doe");
$doc->add($my_data);

请帮帮我。提前致谢。

最佳答案

已弃用多年的 ClientLogin 方法现已关闭。尝试进行身份验证时,它将返回 404。您将需要迁移到 OAuth 以进行身份​​验证。

参见 https://developers.google.com/identity/protocols/AuthForInstalledApps

这是一个使用 OAuth 的更新 PHP 库 - https://github.com/asimlqt/php-google-spreadsheet-client .作者还有一个示例项目,其中包含生成 OAuth 访问 token 的说明。

关于php - 从昨天开始,即 26/05/2015,网站表单数据停止保存到 Google 电子表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com