Value within php array

0

I have the following php structure.

require_once('api.php');

$data = array(
    'DETALLES' => array(
        array(
            'ITEM' => 'N1',
            'DESCRIPCION' => 'DESCRIPCION',
        )
    ),
);


$api = new api();

$result = $api->envioDatos(
    $data,
    'cer',
    array(
        'guardarPDF' => '../comprobantes/documento-'.$result['num_documento'],
    )
);

I have the problem, that in the array (savePDF), only saves the pdf with the name 'document-', not getting the number of the document that I try to obtain and concatenate. this process has to be inside that array since it is predefined from the api.

By doing the process out of $ result () .., I get the document number without problems with $ result ['num_documento'].

I would appreciate your estimated help.

I leave the solution, if the same thing happens to someone.

    require_once('api.php');

    function generarCodigo($longitud) {
    $key = '';
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
    $max = strlen($pattern)-1;
    for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
    return $key;
    }

    $data = array(
            'DETALLES' => array(
                array(
                    'ITEM' => 'N1',
                    'DESCRIPCION' => 'DESCRIPCION',
                )
            ),
        );

        $api = new api();
        $codAleatorio= generarCodigo(6);
        $result = $api->envioDatos(
            $data,
            'cer',
            array(
                'guardarPDF' => '../comprobantes/'.$codAleatorio,
            )
        );
    rename("../comprobantes/".$codAleatorio.".pdf", "../comprobantes/guia-".$resultado['folio'].".pdf");
    rename("../comprobantes/".$codAleatorio.".xml", "../comprobantes/guia-".$resultado['folio'].".xml");
    
asked by JohnL 30.07.2018 в 23:27
source

2 answers

1

You just have to give it a name that you know beforehand and then rename the file, clarify that if the rename does not work, you should try the route well until you get to the file that you saved. I only propose the idea. I have not tried it because I do not have your directory structure or if you are in windows or linux. Here is the solution:

//Creas un nombre unico
$name = uniqid("documento-");
$result = $api->envioDatos(
    $data,
    'cer',
    array(
        'guardarPDF' => '../comprobantes/'.$name, //Le pones el nombre unico
    )
);
//Luego renombras el archivo con el nombre que realmente debe tener
rename(__DIR__.'../comprobantes/'.$name, 'documento-'.$result['num_documento']);
    
answered by 31.07.2018 / 15:25
source
2

I do not understand your problem very well. You can not use $result['num_documento'] within $api->envioDatos() since this function has not returned anything to the variable $result is still unset Can not you get the num_documento first and then use it to send it to the $api->envioDatos function?

    
answered by 31.07.2018 в 00:04