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

d0ef08f0608c

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;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值