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

in #cn8 years ago (edited)

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. xhr.open("POST", url, true);
  3. var myeosaccount = "fundurianyes";
  4. var myname = "test";
  5. var params= "eosaccount="+myeosaccount+"&name="+myname;
  6. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  7. xhr.onreadystatechange = function () {
  8. if (xhr.readyState === 4 && xhr.status === 200) {
  9. var json = JSON.parse(xhr.responseText);
  10. console.log(json.status_message + ", " + json.result);
  11. }
  12. };
  13. 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. $myeosaccount="fundurianyes";
  3. $myname="test";
  4. $mypostdata = "eosaccount=".$myeosaccount."&name=".$myname;
  5. $client = curl_init($url);
  6. curl_setopt($client, CURLOPT_POST, 1);
  7. curl_setopt($client, CURLOPT_POSTFIELDS,$mypostdata);
  8. curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
  9. $response = curl_exec($client);
  10. $result = json_decode($response);
  11. $mymessage= $result->status_message;
  12. $myresult= $result->result;
  13. echo "my message is ".$mymessage;
  14. echo "my result is ".$myresult;
  15. ?>
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>

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hi ~ I'm a robot of yihdashu05630.I just upvoted your post!
Please come visit me here: https://steemit.com/@yihdashu05630
Thanks so much~!!

帅哥/美女!欢迎在steemauto里设置跟赞 @cnbuddy 给整个cn区点赞倘若你想让我隐形,请回复“取消”。

You’ve been upvoted by TeamMalaysia community. Do checkout other posts made by other TeamMalaysia authors at http://steemit.com/created/teammalaysia

To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach

Vote TeamMalaysia witness bitrocker2020 using this link vote for witness

Why dont you use the code block for coding part? Much more readable.

ok, i will try next time

Hi @fundurian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.735 which ranks you at #4799 across all Steem accounts.
Your rank has improved 12 places in the last three days (old rank 4811).

In our last Algorithmic Curation Round, consisting of 450 contributions, your post is ranked at #282.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • You have already convinced some users to vote for your post, keep trying!
  • Your contribution has not gone unnoticed, keep up the good work!

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.105
BTC 64684.13
ETH 1913.44
USDT 1.00
SBD 0.37