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

查看:1435
本文介绍了我应该对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?

或者我只需要URL-编码GET数据?

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标头中的Content-Type来决定。

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.

如果您想了解更多信息,请回答这个问题的答案要详细得多。

This answer has a much more thorough explanation if you'd like more information.

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

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

这里是相关信息:


CURLOPT_POST

TRUE做一个常规的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天全站免登陆