摘要:用php curl 模拟第一篇用html提交的http请求
代码:
$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
代码:
$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--
代码:
$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
代码:
$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请求头。另外示例代码里面有一行是配置代理的,正常使用不用这一项配置。