Function on API


#1
$app->post('/ultima_lectura',function($request,$response){
$input = $request->getParsedBody();
$cups = $input['cups'];
$sqlfechas="SELECT fecha_inicio, fecha_fin XXX al, XXX c WHERE
	    c.id=al.id_contrato AND c.cups_electricidad = :cups ORDER BY fecha_facturacion DESC LIMIT 1";
$sth=$this->db->prepare($sqlfechas);
$sth->bindParam("cups",$input['cups']);
$todos=$sth->fetchAll();
$fecha1 = $todos->fecha_inicio;
$fecha2=$todos->fecha_fin;
$sth->execute();

if ($sth->rowCount()>0){
$electricidad=calcular_termino_electricidad($cups,$fecha1,$fecha2);
   

    function calcular_termino_electricidad($cups,$fecha_inicio,$fecha_fin){
	

    }

});

I am trying to do an API call, and inside my call i need to have a function, but when i try to do the call to the url, i get this error

PHP Fatal error:  Call to undefined function calcular_termino_electricidad()

Is there any way my function works when i make the call?

Thanks


#2

If you define a function within a closure you have to define the function first. Just move the function declaration one line above the function call. Example:

function calcular_termino_electricidad($cups,$fecha_inicio,$fecha_fin)
{
   // ...
}

$electricidad=calcular_termino_electricidad($cups,$fecha1,$fecha2);

#3

Now Im getting error of PHP Fatal error: Using $this when not in object context and im think it is because above my function i have $sth->execute() and inside my function I have this

$sth1 = $this->db->prepare($sql);
$todos1 = $sth1->fetchAll();
$sth1->execute();

any way to by pass that?


#4

I recommend switching to OOP and implementing controllers.

Please read here for more information.


#5

I’m not 100% sure I understand but if you’re function is defined before or outside of your closure, you can probably pass “$this” as an argument, right? Just a guess on my part. YMMV.


#6

I suggest you to dump $this so you can really understand what $this is inside closure,
than take your direction.
you don’t need to move your function inside closure
best practice

  1. use the container

  2. use “use()” statement on closure to pass function.
    hth


#7

So I changed to this. $rfechas=mysqli_query($sqlfechas); and I am getting a response from my code, but getting this errors
PHP Warning: mysqli_query() expects at least 2 parameters, 1 given and

PHP Warning:  mysqli_fetch_row() expects parameter 1 to be mysqli_result, null given

My code is this.

$cups=$_POST['cups'];
$sqlfechas="some sql";
$rfechas=mysqli_query($sqlfechas);
$rrfechas=mysqli_fetch_row($rfechas);
$fecha1=$rrfechas[0];
$fecha2=$rrfechas[1];

if (mysqli_num_rows($rfechas)>0){

    $electricidad=calcular_termino_electricidad($cups,$fecha1,$fecha2);
    if (is_array($electricidad)){
        foreach ($electricidad as $periodo) {
            # code...
            foreach ($periodo as $id_periodo => $fechas) {
                # code...
                foreach ($fechas as $fecha => $datos) {
                    # code...
                    $consumoTotal = $consumoTotal + $datos['consumo'];
                }
            }

        }
        echo json_encode(array('error'=>0, 'lectura'=>$consumoTotal,'fecha_ini'=>$fecha1,'fecha_fin'=>$fecha2));
 echo json_encode(array('error'=>0, 'lectura'=>$consumoTotal,'fecha_ini'=>$fecha1,'fecha_fin'=>$fecha2));
    }
    }else{
    echo json_encode(array('error'=>1));
}

and i am getting error 1, question is now (I know nothing about PHP haha) i am getting echo error 1 because my query doesnt find any data, or because i am not given the mysqli_connect params to mysqli_query and mysqli_fetch_rows?


#8

You have wrong to call mysqli_query function. It should be mysqli_query($param1,$param2);

I never use mysqli before. I just take a look on the error message.