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

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

问题描述

当我使用 curl 通过 POST 并设置 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 =>值配对,并且 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, '', '&');

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

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