Learning Diary,PHP backend API, javascript, unity and PHP frontend call,学习笔记

fundurian(69)
Published in
#cn
Words
647
Reading
3 min
8y
Listen to this post
~4 min

Screen Shot 2018-09-06 at 5.43.04 PM.png
Hello Steemit friends, so today I have learned will record down the PHP backend code and how we can call the backend API using POST method on 3 different language such as Javascript, Unity3D and PHP

So the PHP backend API code look like below where it take 2 input field via POST method and then query the database and then give out the output in JSON format

大家好,最近工作上常用到API call来call去,做个记录

server那里主要是用PHP,接收两个input然后去database寻找答案再output回去,client那里用到三种不同的language,有javascript,Unity3D和PHP

  1. <?php
  2. header("Content-Type:application/json");
  3. include 'pdo_connect.php';
  4. function response($status,$status_message,$result)
  5. {
  6. $response['status']=$status;
  7. $response['status_message']=$status_message;
  8. $response['result']=$result;
  9. $json_response = json_encode($response);
  10. echo $json_response;
  11. }
  12. if(!empty($_POST))
  13. {
  14. $myeosaccount= $_POST['eosaccount'];
  15. $myname = $_POST['name'];
  16. $query = "SELECT * FROM `user` WHERE `eosaccount`= ? and `name`=? ";
  17. $params = array($myeosaccount,$myname);
  18. $results = dataQuery($query, $params);
  19. if($results!=null)
  20. {
  21. $mylevel= $results[0]['publickey'];
  22. response(200,"GET POST RESULT SUCCESS",$mylevel);
  23. }
  24. else
  25. {
  26. response(400,"NOTHING LO",NULL);
  27. }
  28. }
  29. else
  30. {
  31. response(400,"HAIYA NULL WO",NULL);
  32. }
  33. ?>
Code above is PHP backend API, so now we have the backend API ready, we can call it with 3 different kind of language using different kind of syntax

上面的代码是PHP backend server,有了后我们就能用client来call

Javascript:

  1. var xhr = new XMLHttpRequest();
  2. var url = "https://example.com/api/test1.php";
  3. xhr.open("POST", url, true);
  4. var myeosaccount = "fundurianyes";
  5. var myname = "test";
  6. var params= "eosaccount="+myeosaccount+"&name="+myname;
  7. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  8. xhr.onreadystatechange = function () {
  9. if (xhr.readyState === 4 && xhr.status === 200) {
  10. var json = JSON.parse(xhr.responseText);
  11. console.log(json.status_message + ", " + json.result);
  12. }
  13. };
  14. xhr.send(params);
As you can see, in javascript we can use the XMLHttprequest to call the server API and it involve asynchronous call, so you have to wait for the call back on the .onreadystatechange to notify you that output is ready and you can display it on the console

Javascript是用XMLHttprequest来叫server,由于是用asynchronous不等人的call法,所以我们需要用.onreadystatechange 来告诉你几时拿到output

Unity3D:

  1. public void callpost()
  2. {
  3. StartCoroutine(gettoken());
  4. }
  5. public IEnumerator gettoken()
  6. {
  7. WWWForm formToSubmit = new WWWForm();
  8. formToSubmit.AddField("eosaccount", "fundurianyes");
  9. string linkOfPhp = "http://example.com/api/test1.php";
  10. WWW webQuery = new WWW(linkOfPhp, formToSubmit);
  11. yield return webQuery;
  12. Debug.Log(webQuery.text);
  13. }
for Unity3D, you can use the above code on the C# script, and due to it is also a Asynchronous call, thats why we need to use the StartCoroutine method to call the function which in turn will use the WWWForm to call the server API, and finally use the yield return to get the final output

Unity3D是用WWWForm来呼叫,不过也是Asynchronous叫法,所以需要用StartCoroutine和yield return来告诉你几时有output

PHP:

  1. <?php
  2. $url = "https://example.com/api/test1.php";
  3. $myeosaccount="fundurianyes";
  4. $myname="test";
  5. $mypostdata = "eosaccount=".$myeosaccount."&name=".$myname;
  6. $client = curl_init($url);
  7. curl_setopt($client, CURLOPT_POST, 1);
  8. curl_setopt($client, CURLOPT_POSTFIELDS,$mypostdata);
  9. curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
  10. $response = curl_exec($client);
  11. $result = json_decode($response);
  12. $mymessage= $result->status_message;
  13. $myresult= $result->result;
  14. echo "my message is ".$mymessage;
  15. echo "my result is ".$myresult;
  16. ?>
And finally here is the way PHP way of calling the PHP server API by using the curl method where you must specify the CURLOPT_POSTFIELDS and also CURLOPT_RETURNTRANSFER = true

最后是PHP的呼叫法,主要用curl来呼叫,需要设定CURLOPT_POSTFIELDS 和 CURLOPT_RETURNTRANSFER就行了

So hopefully I can go back to this page if I ever forgot the syntax,

thanks for reading

希望以后不记得代码可以回来抄,谢谢阅读

  <br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : http://fundurian.vornix.blog/2018/09/06/learning-diaryphp-backend-api-javascript-unity-and-php-frontend-call%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0/ </em><hr/></center>

Learning Diary,PHP backend API, javascript, unity and PHP frontend ... | Ecency