CURLOPT_POSTFIELDS 的 curl POST 格式 [英] curl POST format for CURLOPT_POSTFIELDS

查看:67
本文介绍了CURLOPT_POSTFIELDS 的 curl POST 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 POST 使用 curl 并设置 CURLOPT_POSTFIELD 时,我是否必须使用 urlencode 或任何特殊格式?

When I use curl via POST and set CURLOPT_POSTFIELD do I have to urlencode or any special format?

例如:如果我想发布 2 个字段,第一个和最后一个:

for example: If I want to post 2 fields, first and last:

first=John&last=Smith

应该与 curl 一起使用的确切代码/格式是什么?

what is the exact code/format that should be used with curl?

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$reply=curl_exec($ch);
curl_close($ch);

推荐答案

如果您要发送字符串,请使用 urlencode() 它.否则,如果是数组,它应该是 key=>value 配对,并且 Content-type 标头自动设置为 multipart/form-data.

In case you are sending a string, urlencode() it. Otherwise if array, it should be key=>value paired and the Content-type header is automatically set to multipart/form-data.

此外,您不必创建额外的函数来为您的数组构建查询,您已经拥有了:

Also, you don't have to create extra functions to build the query for your arrays, you already have that:

$query = http_build_query($data, '', '&');

这篇关于CURLOPT_POSTFIELDS 的 curl POST 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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