我应该使用什么文本编码将CURL用于Google电子表格表单?

时间:2013-01-30 08:26:30

标签: php curl encoding

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 Spreadsheets中的数据时,所有特殊字符,例如ÅÄÖ都缺失了。我猜这是一个编码错误。我该如何更改代码以使其有效?以下是原始表格的样本。

<!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>

1 个答案:

答案 0 :(得分:0)

如果您想模仿html表单使您需要确保的POST请求:

    请求中的
  • Content-Type标头设置为application/x-www-form-urlencoded。由于您设置了CURLOPT_POST=1
  • ,因此CURL已自动完成此操作
  • 您的后期数据采用key1=value&key2=value2格式,其中键和值均为网址编码

在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卷曲的是什么