0

Google 电子表格可以创建允许您将数据发布到电子表格的表单。这不是通过 Google API 完成的。

我正在使用以下代码将数据发布到表单:

<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$googleformURL);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$data = curl_exec ($ch);
curl_close($ch);
//echo $data;
//Redirect to your thank you page
header( "Location: $thankyou" ) ;
?>

这很好用,但是,当我查看 Google 电子表格中的数据时,所有特殊字符(例如 ÅÄÖ)都丢失了。我猜这是一个编码错误。我应该如何更改代码才能使其正常工作?以下是原始表格的示例。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/spreadsheets/forms/favicon_jfk2.png" type="image/x-icon">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Untitled form</title>

<link href='/static/spreadsheets/client/css/779923916-published_form_compiled.css' type='text/css' rel='stylesheet'>
</head>
<body class="ss-base-body" dir="ltr" itemscope itemtype="http://schema.org/CreativeWork/FormObject"><meta itemprop="name" content="Untitled form">



<meta itemprop="embedUrl" content="https://docs.google.com/spreadsheet/embeddedform?formkey=dGJ3ZTdNQ0xwQUJKeGp0dVpDbElVTHc6MQ">
<meta itemprop="faviconUrl" content="//ssl.gstatic.com/docs/spreadsheets/forms/favicon_jfk2.png">

<div class="ss-form-container">
<div class="ss-form-heading"><h1 class="ss-form-title">Untitled form</h1>
<p></p>


<hr class="ss-email-break" style="display:none;">
</div>
<div class="ss-form"><form action="https://docs.google.com/spreadsheet/formResponse?formkey=dGJ3ZTdNQ0xwQUJKeGp0dVpDbElVTHc6MQ&amp;ifq" method="POST" id="ss-form">


<br>
<div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_0">Namn
</label>
<label class="ss-q-help" for="entry_0"></label>
<input type="text" name="entry.0.single" value="" class="ss-q-short" id="entry_0"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_1">Gatuadress
</label>
<label class="ss-q-help" for="entry_1"></label>
<input type="text" name="entry.1.single" value="" class="ss-q-short" id="entry_1"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_2">Postnummer
</label>
<label class="ss-q-help" for="entry_2"></label>
<input type="text" name="entry.2.single" value="" class="ss-q-short" id="entry_2"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_3">Ort
</label>
<label class="ss-q-help" for="entry_3"></label>
<input type="text" name="entry.3.single" value="" class="ss-q-short" id="entry_3"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_4">E-post
</label>
<label class="ss-q-help" for="entry_4"></label>
<input type="text" name="entry.4.single" value="" class="ss-q-short" id="entry_4"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_5">Sort
</label>
<label class="ss-q-help" for="entry_5"></label>
<input type="text" name="entry.5.single" value="" class="ss-q-short" id="entry_5"></div></div></div>
<br> <div class="errorbox-good">
<div class="ss-item  ss-paragraph-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_7">Story
</label>
<label class="ss-q-help" for="entry_7"></label>
<textarea name="entry.7.single" rows="8" cols="75" class="ss-q-long" id="entry_7"></textarea></div></div></div>
<br>
<input type="hidden" name="pageNumber" value="0">
<input type="hidden" name="backupCache" value="">


<div class="ss-item ss-navigate"><div class="ss-form-entry">
<input type="submit" name="submit" value="Submit"></div></div></form>
<script type="text/javascript">

      (function() {
var divs = document.getElementById('ss-form').
getElementsByTagName('div');
var numDivs = divs.length;
for (var j = 0; j < numDivs; j++) {
if (divs[j].className == 'errorbox-bad') {
divs[j].lastChild.firstChild.lastChild.focus();
return;
}
}
for (var i = 0; i < numDivs; i++) {
var div = divs[i];
if (div.className == 'ss-form-entry' &&
div.firstChild &&
div.firstChild.className == 'ss-q-title') {
div.lastChild.focus();
return;
}
}
})();
      </script></div>
<div class="ss-footer"><div class="ss-attribution"></div>
<div class="ss-legal"><span class="ss-powered-by">Powered by <a href="http://docs.google.com">Google Docs</a></span>
<span class="ss-terms"><small><a href="https://docs.google.com/spreadsheet/reportabuse?formkey=dGJ3ZTdNQ0xwQUJKeGp0dVpDbElVTHc6MQ&amp;source=https://docs.google.com/spreadsheet/viewform?formkey%3DdGJ3ZTdNQ0xwQUJKeGp0dVpDbElVTHc6MQ">Report Abuse</a>
-
<a href="http://www.google.com/accounts/TOS">Terms of Service</a>
-
<a href="http://www.google.com/google-d-s/terms.html">Additional Terms</a></small></span></div></div></div></body></html>
4

1 回答 1

0

如果您想模拟 html 表单发出的 POST 请求,您需要确保:

  • Content-Type请求中的标头设置为application/x-www-form-urlencoded. 这已经由 CURL 自动完成,因为您设置CURLOPT_POST=1
  • 您的发布数据采用key1=value&key2=value2格式,其中键和值是 url 编码的

在 PHP 中,只要调用urlencode 键和值字符串(不要对分隔符进行 urlencode&=,当它们充当分隔符时 - 只是键和值)就足够了,只要它们在物理上是 UTF-8 字符串。

PHP 字符串的物理编码完全取决于它的来源。如果您在源文件中逐字写入字符串,则编码取决于 PHP 源文件的编码,该编码是在用于保存文件的文本编辑器的设置中设置的。如果从 MySQL 等数据库中检索字符串,则取决于连接编码。

您可以像这样从数组构建 postdata 字符串:

function arrayToPostData($data) {
    $ret = array();

    foreach( $data as $key => $value ) {
        $ret[] = urlencode($key) . "=" . urlencode($value);
    }

    return implode("&", $ret );
}

在哪里给它一个 UTF-8 编码的键值对数组:

arrayToPostData(array(
        "ö" => "ä",
        "å" => "aaa"
));

如果数组中的字符串物理上是 UTF-8,您将看到正确的结果:

%C3%B6=%C3%A4&%C3%A5=aaa

这是您作为 postdata 传递给 curl 的内容

于 2013-01-30T09:48:26.880 回答