how to send a parameter to a web page in php and return a json [closed]

0

Hello good day I have a doubt I am a newbie in php and I need to send a parameter to a web page which is the following
" link "
but the parameter, which is a hash that I will obtain from a data will be able to perform that kind of web service in php.

    
asked by Oscar Escobar 10.02.2017 в 21:23
source

1 answer

1

Assuming you do not need authentication, the simplest thing would be to have an index.php in link and you could query it do it by ajax or directly, for example: link (and follow) ...

In the index.php you take the values you expect to receive. You create the connection to your database, you perform the query, in a loop you receive the rows and the loads in a vector. This vector should be structured correctly so that at the end you can return it using the function "json_encode ($ vector)".

An extremely simple code would be the following:

// manejador de base de datos
require('db_api.php');
// recibo el parametro
$param1 = $_GET["parametro1"] ?? '';
// array para almacenar los elementos a retornar
$items = array();
// objeto con el cual manejo las consultas a mi tabla usuarios
$dbh = new DB_Usuarios_Tbl();
// esta función "setea" la consulta y limpia los datos recibidos
$dbh->leerDatos($param1);
// voy recibiendo las filas
while($row = $dbh->db_fetch_row()){
    $items[] = $row;
}
// retorno el resultado
print json_encode( $output );

This would be the simplest, the ideal would be a REST service with authentication. There are bookstores for that.

I hope it helps you to have an idea.

    
answered by 11.02.2017 в 03:35