CURL
https://blog.csdn.net/gnnulzy/article/details/79862770详细讲述
https://segmentfault.com/q/1010000007297193//小白自述
针对于跨域请求情况,php curl请求相当于直接访问指定url,不会产生跨域问题
//post方式提交请求
public function curlPost($url, $data, $timeout = 60) {
if ($url == '' || empty($data) || $timeout <= 0) {
return $this->errorPut();
}
$curl = curl_init($url);
$header = ['czadmintoken:value']; //设置一个你的浏览器czadmintoken的header
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, false); // 过滤HTTP头
curl_setopt($curl, CURLOPT_POST, true); // post传输数据格式
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // post传输数据
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 显示输出结果
curl_setopt($curl, CURLOPT_TIMEOUT, (int)$timeout); // 持续时间
$reValue = curl_exec($curl);
curl_close($curl);
return $reValue;
}
//get方式请求 比较简洁
public function httpCurlGet($url, $timeout = 60) {
if ($url == '' || $timeout <= 0) {
return $this->errorPut();
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 显示输出结果
curl_setopt($curl, CURLOPT_TIMEOUT, (int)$timeout); // 持续时间
$reValue = curl_exec($curl);
curl_close($curl);
return $reValue;
}
//模拟post文件上传 接口得到的数据与input上传相同
public function curlupload(){
$url = "www.cz.com/index.php/api/admin/test2";//请求地址
$furl='D:\phpStudy\PHPTutorial\WWW\czContentEditor\public\index.php';上传文件本地路径
$fields=new \CURLFile($furl,'image/jpg','testpicname');
$post_data = array(
"upload" => $fields,
);
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POST, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
p:php版本问题,5.5以前的版本用curl上传文件的时候 文件地址前得加@ 之后的版本得用new \CURLFile($furl,‘image/jpg’,‘testpic’)这个方法