Search code examples
jsonyii2postmanphp-curldspace

Post a not empty file to Dspace with Curl


I am posting a file to dspace through dspace api, I am doing this with an action in php. With postman I send a form-data with the file, filename, bitstream id and token.

enter image description here

I am getting the temp file path with $_FILES and sending it by postfields curl option, but the file uploads empty, or with weird data, without its content.

public function actionSubirBitstream()
    {
        if (Yii::$app->request->isPost) {
            $body = $_POST;

            $prefijo = '/rest/items/';
            $host = ConfiguracionDspace::find()->where("clave='host'")->one()->valor;
            $puerto = ConfiguracionDspace::find()->where("clave='puerto'")->one()->valor;

            $token = $body['token'];
            $id = $body['id'];
            $final = '/bitstreams';

            $name = $body['name'];//nombre del archivo sin espacios
            $params = "?name=$name";
            $url = $host . ':' . $puerto . $prefijo . $id . $final . $params;

            $file = $_FILES['file']['tmp_name'];
            //$path = $body['path'];
            //$file = new CURLFILE("$path");

            $headers = array("Content-Type: application/json", 'Accept: application/json', "rest-dspace-token: " . $token);

            $curl = curl_init($url);
            $options = array(
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array('file'=> $file,'name' => $name),
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => $headers
            );

            curl_setopt_array($curl, $options);
            $response = curl_exec($curl);
            curl_close($curl);
            return $response;
        }
    }

enter image description here


Solution

  • I already post the file by the $_FILE variable, But what I needed to do was to use CURLFILE to create a new file, and give it the temp path of the file in the server $file = $_FILES['file']['tmp_name']; and this in the postfields CURLOPT_POSTFIELDS => array('file'=> new CURLFile("$file"),'name' => $name), Thanks for the help friends

    Here the code:

    public function actionSubirBitstream()
        {
            if (Yii::$app->request->isPost) {
                $body = $_POST;
    
                $prefijo = '/rest/items/';
                $host = ConfiguracionDspace::find()->where("clave='host'")->one()->valor;
                $puerto = ConfiguracionDspace::find()->where("clave='puerto'")->one()->valor;
    
                $token = $body['token'];
                $id = $body['id'];
                $final = '/bitstreams';
    
                $name = $body['name'];//nombre del archivo sin espacios
                $params = "?name=$name";
                $url = $host . ':' . $puerto . $prefijo . $id . $final . $params;
    
                $file = $_FILES['file']['tmp_name'];
    
                //$path = $body['path'];
                //$file = new CURLFILE("$path");
    
                $headers = array("Content-Type: application/json", 'Accept: application/json', "rest-dspace-token: " . $token);
    
                $curl = curl_init($url);
                $options = array(
                    CURLOPT_CUSTOMREQUEST => "POST",
                    CURLOPT_POSTFIELDS => array('file'=> new CURLFile("$file"),'name' => $name),
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_HTTPHEADER => $headers
                );
    
                curl_setopt_array($curl, $options);
                $response = curl_exec($curl);
                curl_close($curl);
                return $response;
            }
        }
    

    The posted files with the rigth size(finally) enter image description here

    The form data I send by Postman, no need to use the field path enter image description here