php的curl扩展, 模拟客户端发出http请求报文
<?php
//1. 初始化curl,返回资源
$curl = curl_init();
//2. 设置curl工具请求的服务器文件地址
//参数1:curl资源
//参数2:设置的请求的选项
//参数3:请求选项的值
//设置将结果返回而不是直接显示
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//跳过百度的https证书的验证
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//不验证
curl_setopt($curl, CURLOPT_HEADER, 0); //不返回header头信息
curl_setopt($curl, CURLOPT_URL, 'https://www.baidu.com/index.php');
//3. 发出请求,接收返回的数据
$result = curl_exec($curl);
echo '<pre>';
var_dump($result);
//4. 关闭curl资源
curl_close($curl);
POST请求
//1. 初始化curl,返回资源
$ch = curl_init();
//2. 设置选项,设置请求的服务器地址
curl_setopt($ch, CURLOPT_URL, 'http://localhost/0420curl/3.post_handle.php');
//开启post提交
curl_setopt($ch, CURLOPT_POST, true);
//设置提交什么数据过去
$data = [
'order'=>'ESC1000012',
//>=php5.6
'img'=> new \CURLFile('/path/to/x.jpg'),
//<php5.6
'img'=>'@/path/to/x.jpg',
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//设置cookie存储路径
curl_setopt($ch, CURLOPT_COOKIEJAR, '/path/to/x.cookie');
//设置cookie读取路径
curl_setopt($ch, CURLOPT_COOKIEFILE, '/path/to/x.cookie');
//3. 发出请求
curl_exec($ch);
//4. 关闭资源
curl_close($ch);