dsbc80836 2016-09-26 11:48
浏览 269

为什么cURL在POST时解码我的URL编码值?

When using cURL to send data via POST, if that data string is URL encoded or if parts of it are URL encoded, cURL automatically decodes the data when sending it.

This happens when using cURL in PHP or directly in the command line.

I've tested with 2 different version of cURL: 7.19 and 7.49. Both exhibit the same behavior

I've sent the cURL request from two different servers thinking that the the way the servers was configured somehow influenced this, but the result was the same.

Here is a simple PHP cURL request that I've used for my test:

$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13';

$data = "https%3A%2F%2Fexample.com%3A8081%2Ftemoignez%3FQid%3D%26"       

$ch = curl_init( "https://example.com/test/webhook.php" );
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "payload=".$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

The data will be sent decoded even though the initial string is URL encoded.

I'm retrieving the data by dumping the POST data into a file on disk using PHP.

Is this normal? Any idea what may cause this?

  • 写回答

1条回答 默认 最新

  • duanchuonong5370 2016-09-26 12:34
    关注

    You have two different assertions here:

    cURL automatically decodes the data when sending it.

    ...

    I've simply dumped the POST data into a file after retrieving it.

    It is PHP that automatically DECODES the data when receiving it. It is NOT getting decoded upon sending it!

    This integrates with the behaviour of other values, like cookie data, post and get variables, header information like referrer, ... everything get's decoded automatically when it is received, because it is expected to be sent encoded.

    When you want to see the exact data that is getting send over the wires, use a tool like ngrep on port 80 to sniff the TCP HTTP traffic.

    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题