上传图片
$url = "http://localhost/v1/accident/upload-image";
$file = realpath('_mars.jpg'); //要上传的文件
$data = array(
'uid'=>'123',
'appcode'=>'bhwhc',
'file' => '@'.$file,
'flg' => 2
);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_POST, true );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
var_dump($json_obj);exit;
restful 接口
public function actionUploadImage()
{
if(Yii::$app->request->isGet)
{
$request = Yii::$app->request->get();
}elseif (Yii::$app->request->isPost)
{
$request = Yii::$app->request->post();
}
$file = UploadedFile::getInstance($model,'file');
//return $request;
$uid = $request['uid'];
$appcode = $request['appcode'];
if(empty($uid) || empty($appcode))
{
return ['code'=>'1001','message'=>'参数缺失'];
}
$userinfo = KckpUser::findOne(['app_user_id'=>$uid,'app_code'=>$appcode]);
if(empty($userinfo))
{
$model = new KckpUser();
$model->app_user_id = $uid;
$model->app_code = $appcode;
$model->create_time = date('Y-m-d H:i:s');
$model->save();
}
if($_FILES['file'])
{
//$filename = $_FILES['file']['name'];
return ['code'=>'200','message'=>'有数据上传'];
}else{
return ['code'=>'400','message'=>'无文件上传'];
}
$modelClass = $this->modelClass;
$query = $modelClass::find();
return new ActiveDataProvider([
'query' => $query
]);
}
不知道那里有问题,一直都是没有文件上传
查看php版本,5.5前是用’@’+文件全路径,5.5后用CURLFile类。
你先一步步打印,看程序走到哪里了?
能打印出来上传的文件资源吗