php curl中x-www-form-urlencoded与multipart/form-data 方式 Post 提交数据详解

multipart/form-data 方式

post的curl库,模拟post提交的时候,默认的方式 multipart/form-data ,这个算是post提交的几个基础的实现方式。

$postUrl = '';
$postData = array(
    'user_name'=>$userName,
    'identity_no'=>$idCardNo
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl); 
curl_close($curl);

print_r($r);

想用的可以直接拿去试试

x-www-form-urlencoded方式

php的curl库进行post提交还是蛮方便的。但是提交方式不同,contentType 不同导致你的api是否能接收到数据也是个变数,这里来个简单的实例。

$postUrl = '';
$postData = array(
    'user_name'=>$userName,
    'identity_no'=>$idCardNo
);
$postData = http_build_query($postData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl); 
curl_close($curl);

print_r($r);

关键一段代码是

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索windows , 数据 , array opera curlformadd 参数详解、urlencodedformentity、xwwwform urlencoded、formurlencoded、wwwform urlencoded,以便于您获取更多的相关知识。

转载于:https://www.cnblogs.com/jshen/p/9711768.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,如果你需要在C语言使用curl库发送multipart/form-data格式的POST请求,可以参考以下步骤: 1. 创建一个curl实例,并设置请求地址和请求方法POST。 2. 设置请求头信息,包括Content-Type和boundary。 3. 组装请求参数,将需要上传的文件转化为二进制数据,并按照multipart/form-data格式进行拼接。 4. 使用curl_easy_setopt函数设置CURLOPT_POSTFIELDS选项,将请求参数作为POST请求的数据发送。 5. 调用curl_easy_perform函数发送请求并获取响应。 下面是一个示例代码,假设需要上传一个名为file.txt的文件: ```c #include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: multipart/form-data; boundary=------------------------1234567890"); curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/upload"); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); const char *boundary = "------------------------1234567890"; const char *filename = "/path/to/file.txt"; FILE *fp = fopen(filename, "rb"); fseek(fp, 0, SEEK_END); long file_len = ftell(fp); fseek(fp, 0, SEEK_SET); char *file_data = malloc(file_len); fread(file_data, file_len, 1, fp); fclose(fp); char *request_data; asprintf(&request_data, "--%s\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "Content-Type: application/octet-stream\r\n" "\r\n" "%s\r\n" "--%s--\r\n", boundary, filename, file_data, boundary); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data); res = curl_easy_perform(curl); curl_slist_free_all(headers); curl_easy_cleanup(curl); free(request_data); if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } } curl_global_cleanup(); return 0; } ``` 在示例代码,我们创建了一个名为curlcurl实例,并设置请求地址和请求方法POST。然后,我们设置了请求头信息,包括Content-Type和boundary。接着,我们读取文件内容并组装请求参数,最后使用curl_easy_setopt函数设置CURLOPT_POSTFIELDS选项,将请求参数作为POST请求的数据发送。最后,我们调用curl_easy_perform函数发送请求并获取响应。 你可以根据自己的需求进行更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值