ThinkPHP CURL上传

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’)这个方法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值