calling a php function from c#

Im trying to acced to a database via PHP

i have seen post that i have to call the PHP file via WWW
im doing this:

IEnumerator llama_php(){
     

        string textodd="";
      
        string url="file:///C:/xampp/htdocs/webservice/conexion_juan.php?f=utf8_converter";
         using (WWW www = new WWW(url))
        {
            yield return www;
            textodd=""+www.text;
            Debug.Log("descargando...");
        }

utf8_converter is the function on PHP that i need to call

this is the php file

<?php
  class conexion{
    public function conectar(){
      $hostname = 'localhost';
      $database = 'prueba_uno';
      $username = 'root';
      $password = 'password';
      $mysqli = new mysqli($hostname, $username,$password, $database);
      if($mysqli -> connect_errno){
        die( "Fallo la conexión a MySQL: (" .$mysqli -> mysqli_connect_errno(). ") " . $mysqli -> mysqli_connect_error());
           return false;
       }
      else{
           return $mysqli;
      }
    }
    #Este metodo devuelve recibe un arreglo y lo codifica en utf8 para no causar problemas en json
    function utf8_converter($array){
      array_walk_recursive($array, function(&$item, $key){
          if(!mb_detect_encoding($item, 'utf-8', true)){
                  $item = utf8_encode($item);
          }
      });

      return $array;
    }
  }
?>

but instead of give m the array, or a string with only the db array, it returns me the whole code in an string
“<?php
class conexion{…”

can you help me please?

This is a web server configuration issue, please consult with the manual of the webserver of your choice or try to get help on their forums.

I believe you’ll need to call the PHP file via http and not file. You generally do not want to connect directly to a database like this, but instead want to implement a web api service.

2 Likes

Ouch, I didn’t catch that, I need a coffee, apparently. :smile:

1 Like