Search Unity

[UnityWebRequest] Api connexion without using php script

Discussion in 'Scripting' started by Simon75012, Jun 23, 2019.

  1. Simon75012

    Simon75012

    Joined:
    Sep 15, 2016
    Posts:
    79
    Hi, i'm connecting to an API using a php script on my server with UnityWebRequest.

    My php script :
    Code (CSharp):
    1.  
    2.     $apikey=$_POST['apikey'];
    3.     $apisecret=$_POST['apisecret'];
    4.     $nonce=time();
    5.     $uri='https://apiurl.com/api/getdata?apikey='.$apikey.'&nonce='.$nonce;
    6.     $sign=hash_hmac('sha512',$uri,$apisecret);
    7.     $ch = curl_init($uri);
    8.     curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
    9.     $execResult = curl_exec($ch);
    10.     $obj = json_decode($execResult);
    11.  
    My C# script on Unity :
    Code (CSharp):
    1.     string url = "https://myserver.com/abc.php";
    2.         WWWForm form = new WWWForm();
    3.         form.AddField("apikey",data.ApiKey);
    4.         form.AddField("apisecret",data.ApiSecret);
    5.         UnityWebRequest uwr = UnityWebRequest.Post(url,form);
    6.         yield return uwr.SendWebRequest();
    7.         if(uwr.isNetworkError || uwr.isHttpError){
    8.             Debug.Log(uwr.error);
    9.         }else{
    10.             Debug.Log(uwr.downloadHandler.text);
    Do you think it's possible to avoid using the php script and connect directly to the API server and save some time?
    Thanks.