我应该对 POST 数据进行 URL 编码吗? [英] Should I URL-encode POST data?

查看:44
本文介绍了我应该对 POST 数据进行 URL 编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据发布到外部 API(使用 PHP,如果相关的话).

I'm POSTing data to an external API (using PHP, if it's relevant).

我应该对传递的 POST 变量进行 URL 编码吗?

Should I URL-encode the POST variables that I pass?

还是我只需要对 GET 数据进行 URL 编码?

Or do I only need to URL-encode GET data?

更新:这是我的 PHP,以防万一:

UPDATE: This is my PHP, in case it is relevant:

$fields = array(
    'mediaupload'=>$file_field,
    'username'=>urlencode($_POST["username"]),
    'password'=>urlencode($_POST["password"]),
    'latitude'=>urlencode($_POST["latitude"]),
    'longitude'=>urlencode($_POST["longitude"]),
    'datetime'=>urlencode($_POST["datetime"]),
    'category'=>urlencode($_POST["category"]),
    'metacategory'=>urlencode($_POST["metacategory"]),
    'caption'=>($_POST["description"])
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

推荐答案

一般答案

您的问题的一般答案是视情况而定.你可以通过指定你的内容类型"来决定.位于 HTTP 标头中.

General Answer

The general answer to your question is that it depends. And you get to decide by specifying what your "Content-Type" is in the HTTP headers.

application/x-www-form-urlencoded"的值意味着您的 POST 正文将需要像 GET 参数字符串一样进行 URL 编码.multipart/form-data"的值意味着您将使用内容分隔符,而不是对内容进行 url 编码.

A value of "application/x-www-form-urlencoded" means that your POST body will need to be URL encoded just like a GET parameter string. A value of "multipart/form-data" means that you'll be using content delimiters and NOT url encoding the content.

如果您想了解更多信息,这个答案有更详尽的解释.

对于特定于您使用的 PHP 库的答案 (CURL),您应该 阅读此处的文档.

For an answer specific to the PHP libraries you're using (CURL), you should read the documentation here.

以下是相关信息:

CURLOPT_POST

执行常规 HTTP POST 为真.此 POST 是普通的 application/x-www-form-urlencoded 类型,最常用于 HTML 表单.

TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

CURLOPT_POSTFIELDS

要在 HTTPPOST"中发布的完整数据手术.要发布文件,请在文件名前加上 @ 并使用完整路径.文件类型可以通过在文件名后面加上格式;type=mimetype"的类型来明确指定.此参数可以作为 urlencoded 字符串(如 'para1=val1&para2=val2&...')传递,也可以作为以字段名称作为键和字段数据作为值的数组传递.如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data.从 PHP 5.2.0 开始,如果文件以 @ 前缀传递给此选项,则 value 必须是数组.

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

这篇关于我应该对 POST 数据进行 URL 编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆