600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php curl跨域cookie_PHP curl模拟文件上传(接口请求实现跨域文件中转)

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

时间:2023-06-28 10:35:33

相关推荐

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

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('ment');

$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('ment');

$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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。