http请求头 Content-Type 详解(二)

摘要:用php curl 模拟第一篇用html提交的http请求

一、 Content-Type: application/x-www-form-urlencoded

代码:

$post_url = "http://localhost/test/curl/service.php";
$post_data = array(
    'a' => 'b',
    'c' => 'd'
);
$post_str = http_build_query($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

http请求:

POST /test/curl/service.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 7
Content-Type: application/x-www-form-urlencoded

a=b&c=d

二、Content-Type: multipart/form-data;

代码:

$post_url = "http://localhost/test/curl/service.php";
$post_data = array(
    'a' => 'b',
    'c' => 'd'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

http请求:

POST /test/curl/service.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 228
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------334b80ff0035ad34

--------------------------334b80ff0035ad34
Content-Disposition: form-data; name="a"

b
--------------------------334b80ff0035ad34
Content-Disposition: form-data; name="c"

d
--------------------------334b80ff0035ad34--

三、Content-Type: multipart/form-data; 模拟文件提交

代码:

$post_url = "http://localhost/test/curl/service.php";
$post_data = array ("a" => "a","b" => "b","img" => new CURLFile(realpath('./barimage.bmp')));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

http请求:

POST /test/curl/service.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 5196
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------ad33a9cd47c155c8

--------------------------ad33a9cd47c155c8
Content-Disposition: form-data; name="a"

a
--------------------------ad33a9cd47c155c8
Content-Disposition: form-data; name="b"

b
--------------------------ad33a9cd47c155c8
Content-Disposition: form-data; name="img"; filename="D:\\www\\test\\curl\\barimage.bmp"
Content-Type: application/octet-stream

(二进制串)
--------------------------ad33a9cd47c155c8

四、Content-Type: application/json;charset=utf-8 发送json串

代码:

$post_url = "http://localhost/test/curl/service.php";
$post_data = array(
    'a' => 'b',
    'c' => 'd'
);
$post_str = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json;charset=utf-8",
    "User-Agent: ua"
));
$output = curl_exec($ch);

http请求:

POST /test/curl/service.php HTTP/1.1
Host: localhost
Accept: */*
Content-Type: application/json;charset=utf-8
User-Agent: ua
Content-Length: 17

{"a":"b","c":"d"}

总结:使用php curl 做http请求,有php语言原生的影子,比如1、2一个穿字符串,一个传php数组,最终http请求的content-type就不一样。当然也可以直接指定content-type,就是用CURLOPT_HTTPHEADER这个配置项来配置http请求头。另外示例代码里面有一行是配置代理的,正常使用不用这一项配置。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值