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('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;
}
總結
以上是生活随笔為你收集整理的php curl跨域cookie_PHP curl模拟文件上传(接口请求实现跨域文件中转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 队列的基本操作_算法与数据结构(五) 栈
- 下一篇: android指纹java_Androi