PHP的CURL使用大全
CURL的GET、POST公共方法
- 创建一个类文件:CurlController
- 写一个公共方法curlResource():
class CurlController {
public function curlResource($url, $data, $method, $type= '')
{
$ch = curl_init();
$headers = array(
'json' => ['Content-Type: application/json'], //json格式
'form-data' => ['Content-Type: multipart/form-data'], //数组格式
'string' => ['Content-Type: application/x-www-form-urlencoded'] //键值对格式
);
if($method == 'GET'){
if($data){
$url = $url.'?'.$data;
}
} else {
if ($type) {
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers[$type]);
}
}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行后不直接打印出来
if($method == 'POST'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST'); // 请求方式
curl_setopt($ch, CURLOPT_POST, true); // post提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // post的变量
}
if($method == 'PUT'){
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
}
if($method == 'DELETE'){
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
$output = curl_exec($ch); //执行并获取返回内容
curl_close($ch); //释放curl句柄
return json_decode($output, true);
}
}
GET方法
- 在类CurlController中写个方法getRequest()进行GET请求:
public function getRequest()
{
$data = array(
'name' => 'hai',
'age' => 29,
'sex' => '男',
);
$data = http_build_query($data); //生成键值对参数
$url = 'http://www.jwt.com/api/img'; //请求链接地址
$result = $this->curlResource($url, $data, 'GET');
dump($result); 打印返回的信息
}
POST方法
- Content-Type: application/x-www-form-urlencoded //键值对格式
public function postString()
{
$data = array(
'name' => 'hai',
'age' => 29,
'sex' => '男',
);
$data = http_build_query($data); //生成键值对参数
$url = 'http://web.jwt.com/api/img';
$result = $this->curlResource($url, $data, 'POST', 'string');
dump($result);
}
- Content-Type: application/json //json格式
public function postJson()
{
$data = array(
'name' => 'hai',
'age' => 29,
'sex' => '男',
);
$data = json_encode($data); //生成json数据
$url = 'http://web.jwt.com/api/img';
$result = $this->curlResource($url, $data, 'POST', 'json');
dump($result);
}
- Content-Type: multipart/form-data //文件上传格式
public function uploadImg()
{
$filePath = "/public/lazyload.jpg"; //上传文件的绝对路径
$img = $this->makeCurlFile($filePath); //实例化图片数据
$data = array(
'name' => 'zhuLaiLai DEL', //额外参数
'file' => $img, //文件信息
);
$url = 'http://web.jwt.com/api/img';
$result = $this->curlResource($url, $data, 'POST', 'form-data');
dump($result);
}
private function makeCurlFile($file)
{
$fi = new \finfo(FILEINFO_MIME_TYPE);
$mime = $fi->file($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new \CURLFile($file, $mime, $name);
return $output;
}
CURL文件下载
public function imgDown()
{
$url = 'http://web.jwt.com/wx.xlsx'; //下载的文件地址
$savePath = './images/down/'.date('Y/m/d/'); //文件下载目录
if (!is_dir($savePath)) {
@mkdir($savePath, 0777, true);
@chmod($savePath, 0777);
}
$arr = explode('/', $url); //获取文件名称
$filename = array_pop($arr);
$savePath .= $filename;
echo $this->download($url, $savePath);
}
public function download($url, $filename)
{
$ch = curl_init (); //初始化
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); //设置请求方式
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //执行后不直接打印数据,返回给一个变量
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); //跳过证书检查
curl_setopt ( $ch, CURLOPT_URL, $url ); //链接地址
$return_content = curl_exec ( $ch ); //获取返回的数据
$return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); //获取header头的最后字符串
curl_close($ch);
if ($return_code == '200') {
$fp= @fopen($filename,"a");
fwrite($fp,$return_content);
return true;
} else {
return $return_code;
}
}