Run PHP function from C #

1

I have a REST API with a function that returns a number (folio for me) and adds 1 and so on, if I execute that function from Postam using the API URL, it returns what I want, until here everything very well. Now as I command to execute that function from c # ?, I would not pass parameters to it, I just want to execute the function and the returned response is a json_encode (message). I understand that it can be done with webclient downloadstring (url api) but it does not return the json, it only returns null. Any serious help welcome. Thanks again.

Here's the code in PHP:

else if($_GET['url'] == "folio")
{
  $respuesta = Clientes::ObtenerFolio();
  $contenedor = array();
  if($respuesta)
  {
    $contenedor["resultado"]="OK";
    $contenedor["datos"]= $respuesta;
    echo json_encode($contenedor);
  }
  else
  {
    echo json_encode(array(
        "resultado" => 'NONE',
        "mensaje" =>'No se pudo asignar folio al cliente'
    ));
  }

This is from C # winforms:

try
{
  string responsebody;

  HttpClient cliente = new HttpClient();
  HttpResponseMessage response = await cliente.GetAsync("http://www.mipagina.mx/WebService/folio");
  response.EnsureSuccessStatusCode();
  responsebody = await response.Content.ReadAsStringAsync();
  dynamic datosdes = JsonConvert.DeserializeObject(responsebody);
  string mjsserver = datosdes.resultado;
  string msjservernot = datosdes.datos;
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}
    
asked by Manny 07.08.2018 в 17:31
source

2 answers

0

Well thanks to those who commented, everything in C # was correct, but in PHP I was receiving everything for $_SERVER['REQUEST_METHOD'] == "POST" and it is clear that from C # I do GetAsync because I would never return "something", so I did an else if after the POST asi $_SERVER['REQUEST_METHOD'] == "GET" and there I put it in

if($_GET['url'] == "folio")
        {
            $respuesta = Clientes::ObtenerFolio();
            $contenedor = array();
            header('Content-Type: application/json');
            if($respuesta)
            {
                $contenedor["resultado"]="OK";
                $contenedor["datos"]= $respuesta;
                echo json_encode($contenedor);
            }
            else
            {
            echo json_encode(array("resultado" => 'NONE',"mensaje" =>'No se pudo asignar folio al cliente'));
            }
        }

and this I return the json and we swam the deserializo in C # and ready.     Thanks to everyone.

    
answered by 07.08.2018 / 21:39
source
0

this: link ", do you return any results? if it does not work then your rest point is wrong

It sounds to me that what you have to do is something like this:

 http://www.mipagina.mx/WebService.php?url="folio"
    
answered by 07.08.2018 в 20:53