在postman中使用php curl将文件上载到Nextcloud,但上载的文件为空

在postman中使用php curl将文件上载到Nextcloud,但上载的文件为空,php,curl,yii2,postman,nextcloud,Php,Curl,Yii2,Postman,Nextcloud,解决 这是我的代码,我正试图使用其api将一个文件上载到nextcloud,我上载了该文件,但它是空的 我所做的是使用fopen和fread保存文件内容,并通过postfields将其发送到nextcloud: public function actionSubirArchivoNube() { $response = null; if(Yii::$app->request->isPost){ $body = Yii:

解决 这是我的代码,我正试图使用其api将一个文件上载到nextcloud,我上载了该文件,但它是空的

我所做的是使用fopen和fread保存文件内容,并通过postfields将其发送到nextcloud:

public function actionSubirArchivoNube()
    {
        $response = null;
        if(Yii::$app->request->isPost){

            $body = Yii::$app->request->getRawBody();
            $body = Json::decode($body);


            $datosNube = $body['CredencialesNube'];
            $username = $datosNube['username'];
            $password = $datosNube['password'];
            $servidorNube = $datosNube['server_name'];
            $camino = $datosNube['pathArchivo'];
            $filename = basename($camino);

            //Se tiene el contenido del archivo
            $gestor = fopen($camino, "r");
            $contenido = fread($gestor, filesize($camino));
            fclose($gestor);

            //Se tiene la url que responde a la nube y los headers
            $url = $servidorNube .'/remote.php/dav/files/admin/' . $filename;
            $headers = array('Authorization: Basic ' . base64_encode("$username:$password"),
                'OSC-APIRequest: true', 'Content-Type: text/html; charset=UTF-8');

            $options = array(
                CURLOPT_SAFE_UPLOAD => true,
                CURLOPT_HEADER => true,
                CURLOPT_CUSTOMREQUEST => "PUT",
                CURLOPT_URL => $url,
                CURLOPT_HTTPHEADER => $headers,
                CURLOPT_POSTFIELDS => $contenido,
                CURLOPT_SSL_VERIFYPEER=> false
            );

            $curl = curl_init();
            curl_setopt_array($curl, $options);
            $response = curl_exec($curl);
            curl_close($curl);
            $response = json_decode($response,true);

            return $response;
        }       
    }

取决于您在使用邮递员时是使用
POST
还是
GET
请求,上述代码将显示状态
200 ok
,即使请求是
GET
,但不会执行任何操作,除非
POST
添加else块并引发异常。请求不会作为例外处理,POST请求应包括
submit
这取决于使用时是否使用
POST
GET
请求邮递员,上述代码将显示状态
200 ok
,即使请求是
GET
,但除非
POST
添加else块并抛出异常,否则不会执行任何操作。请求不会作为例外,POST请求应包括
submit