Change PHP STATUS to pick it up from Unity

0

I have a file in php where I receive data from an application in Unity, all good until after validating the information I need to recover a STATUS .

I'm using this way of setting the STATUS in php ;

if(count(mysql_fetch_array(mysql_query($sqlSelectNombUser,$cone)))>1){

$result="no valido";
http_response_code(400);

}else{

 $result="valido";
 $rsInsertNew=mysql_query($sqlInsertNew,$cone);
 http_response_code(200);

}

And to recover it from Unity I use;

public static int getResponseCode(WWW request)
{
    int ret = 0;

    if (request.responseHeaders == null)
    {
        Debug.LogError("no response headers.");
    }
    else
    {

        if (!request.responseHeaders.ContainsKey("STATUS"))
        {
            Debug.LogError("response headers has no STATUS.");
        }
        else
        {
            ret = parseResponseCode(request.responseHeaders["STATUS"]);
        }
    }

    return ret;
}

public static int parseResponseCode(string statusLine)
{
    int ret = 0;

    string[] components = statusLine.Split(' ');
    if (components.Length < 3)
    {
        Debug.LogError("invalid response status: " + statusLine);
    }
    else
    {
        if (!int.TryParse(components[1], out ret))
        {
            Debug.LogError("invalid response code: " + components[1]);
        }
    }

    return ret;
}

The fact is that I can not change STATUS since I always receive the 200, even having forced the failure directly in PHP .

How could I change the STATUS to recover it in Unity?

    
asked by Hector Lopez 02.11.2018 в 17:56
source

1 answer

1

I use this Json library to facilitate my work: SimpleJSON: JSON Library for Unity

And the example of use would be:

On the button or place where you would call:

StartCoroutine (iniPost ());

And to do the post:

IEnumerator iniPost (){ 
    // Creamos la forma para hacer el post
    var form = new WWWForm();
    form.AddField ("categoria", "1");//Agregamos variables necesarias
    var url = "http://direccionalPost.com/test.php";
    var d = new WWW( url, form );
    // Llamamos método para escuchar la respuesta del post
    StartCoroutine(WaitForRequest(d));
    if(d == null)
        yield return null;
}

To hear the answer:

IEnumerator WaitForRequest(WWW www){
    yield return www;
    // buscamos errores
    if (www.error == null){//No hubo error en el post
        Debug.Log( "Json " + www.text );
        au = JSON.Parse (www.text);//Convertimos la respuesta a Json con la librería SimpleJson, hacer el import con el Using SimpleJSON;
        if (au ["error"] != null && au ["error"].ToString ().Length > 0) {//Localmente validadmos que no se haya enviado error nuestro, en caso de que exista la llave lo imprimimos

            Debug.Log ( au ["error"]);
        } else {
            Debug.Log ("No hubo error buscamos llaves necesarias");

        }
    } else {
        Debug.Log ("Error en el post: " + www.error);
    }    
}   

What you should answer in your php when there is an error would be:

{"error":"No valido", "status_code":"400"}

When there is no error:

{"status_code":"200"}

And it would be for you to add the other things you need ...

    
answered by 06.11.2018 / 19:47
source