美文网首页
PHP curl模拟文件上传(接口请求实现跨域文件中转)

PHP curl模拟文件上传(接口请求实现跨域文件中转)

作者: poplanchong | 来源:发表于2016-12-14 13:45 被阅读401次
3e2f08c0c11a8416dd107bbfc9159718.jpg
客户端代码
请求参数 参数类型 参数说明
$url string post提交的服务器url路径
$data 数组 表单数据
$files 数组 表单文件
    public function curl_custon_postfields($url, array $data = array(), array $files = array())
    {
        $curlfiles = array();
        foreach ($files as $key => $value) {
            $tmpname = $files[$key]['name'];
            $tmpfile = $files[$key]['tmp_name'];
            $tmpType = $files[$key]['type'];

            $curlfiles[$key] = new CURLFile(realpath($tmpfile), $tmpType, $tmpname);
        }

        $dataTotal = array_merge($data, $curlfiles);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_USERPWD, 'joe:secret');
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataTotal);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return_data = curl_exec($ch);
        curl_close($ch);
        echo $return_data;
    }

调用

    public function evaluate()
    {
        $tag = 'evaluate';
        $code = 1000;
        $data = array();

        $token = I('post.token');
        $order_id = I('post.order_id');
        $star = I('post.star');
        $tag2 = I('post.tag');
        $comment = I('post.comment');

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传(子)目录
        $upload->autoSub = true;
        $upload->subName = 'comment';
        $info = $upload->upload();

        if (!$info) {

            $info = array('response' => $tag, 'code' => 1, 'msg' => $upload->getError(), 'data' => $data);
            echo json_encode($info);
            exit;

        } else {
            if (count($info) > 3) {

                $info = array('response' => $tag, 'code' => 1, 'msg' => '最多只能上传3张图片', 'data' => $data);

                echo json_encode($info);
                exit;
            }

            foreach ($info as $key => $file) {
                $images[$key] = C('site_url') . '/Uploads / ' . $file['savepath'] . $file['savename'];
            }
        };


        $info = array('response' => $tag, 'code' => 1000, 'msg' => '成功', 'data' => $images);

        echo json_encode($info);
        exit;

    }

服务器端代码

    public function evaluate()
    {
        $tag = 'evaluate';
        $code = 1000;
        $data = array();

        $token = I('post.token');
        $order_id = I('post.order_id');
        $star = I('post.star');
        $tag2 = I('post.tag');
        $comment = I('post.comment');

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传(子)目录
        $upload->autoSub = true;
        $upload->subName = 'comment';
        $info = $upload->upload();

        if (!$info) {

            $info = array('response' => $tag, 'code' => 1, 'msg' => $upload->getError(), 'data' => $data);
            echo json_encode($info);
            exit;

        } else {
            if (count($info) > 3) {

                $info = array('response' => $tag, 'code' => 1, 'msg' => '最多只能上传3张图片', 'data' => $data);

                echo json_encode($info);
                exit;
            }

            foreach ($info as $key => $file) {
                $images[$key] = C('site_url') . '/Uploads / ' . $file['savepath'] . $file['savename'];
            }
        };


        $info = array('response' => $tag, 'code' => 1000, 'msg' => '成功', 'data' => $images);

        echo json_encode($info);
        exit;

    }

参考:
1 大神文档
2 php官网

相关文章

网友评论

      本文标题:PHP curl模拟文件上传(接口请求实现跨域文件中转)

      本文链接:https://www.haomeiwen.com/subject/ewuimttx.html