php模拟HTTP协议发送post请求方法

今天用到php模拟http发送post请求记录

代码如下:

<?php
$url = 'xxxx.com';
$data = 'a=one&b=two';
$data = urlencode($data);
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//抓取网页
curl_setopt($ch, CURLOPT_POST, 1);//设置post
curl_setopt($ch, CURLOPT_POSTFILES, $data);//post数据写入,全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径。这个参数可以通过urlencoded后的字符串类似'para1=val1&para2=val2&...'或使用一个以字段名为键值,字段数据为值的数组。如果value是一个数组,Content-Type头将会被设置成multipart/form-data。
$re = curl_exec($ch);//执行cURL会话;
curl_close($ch);//关闭会话,释放资源;
?>
原文地址:https://www.cnblogs.com/lilili/p/5122689.html