Learning Diary,PHP backend API, javascript, unity and PHP frontend call,学习笔记
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
-
<?php
-
header("Content-Type:application/json");
-
include 'pdo_connect.php';
-
function response($status,$status_message,$result)
-
{
-
$response['status']=$status;
-
$response['status_message']=$status_message;
-
$response['result']=$result;
-
$json_response = json_encode($response);
-
echo $json_response;
-
}
-
if(!empty($_POST))
-
{
-
$myeosaccount= $_POST['eosaccount'];
-
$myname = $_POST['name'];
-
$query = "SELECT * FROM `user` WHERE `eosaccount`= ? and `name`=? ";
-
$params = array($myeosaccount,$myname);
-
$results = dataQuery($query, $params);
-
if($results!=null)
-
{
-
$mylevel= $results[0]['publickey'];
-
response(200,"GET POST RESULT SUCCESS",$mylevel);
-
}
-
else
-
{
-
response(400,"NOTHING LO",NULL);
-
}
-
}
-
else
-
{
-
response(400,"HAIYA NULL WO",NULL);
-
}
-
?>
上面的代码是PHP backend server,有了后我们就能用client来call
Javascript:
-
var xhr = new XMLHttpRequest();
-
var url = "https://example.com/api/test1.php";
-
xhr.open("POST", url, true);
-
var myeosaccount = "fundurianyes";
-
var myname = "test";
-
var params= "eosaccount="+myeosaccount+"&name="+myname;
-
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
xhr.onreadystatechange = function () {
-
if (xhr.readyState === 4 && xhr.status === 200) {
-
var json = JSON.parse(xhr.responseText);
-
console.log(json.status_message + ", " + json.result);
-
}
-
};
-
xhr.send(params);
Javascript是用XMLHttprequest来叫server,由于是用asynchronous不等人的call法,所以我们需要用.onreadystatechange 来告诉你几时拿到output
Unity3D:
-
public void callpost()
-
{
-
StartCoroutine(gettoken());
-
}
-
public IEnumerator gettoken()
-
{
-
WWWForm formToSubmit = new WWWForm();
-
formToSubmit.AddField("eosaccount", "fundurianyes");
-
string linkOfPhp = "http://example.com/api/test1.php";
-
WWW webQuery = new WWW(linkOfPhp, formToSubmit);
-
yield return webQuery;
-
Debug.Log(webQuery.text);
-
}
Unity3D是用WWWForm来呼叫,不过也是Asynchronous叫法,所以需要用StartCoroutine和yield return来告诉你几时有output
PHP:
-
<?php
-
$url = "https://example.com/api/test1.php";
-
$myeosaccount="fundurianyes";
-
$myname="test";
-
$mypostdata = "eosaccount=".$myeosaccount."&name=".$myname;
-
$client = curl_init($url);
-
curl_setopt($client, CURLOPT_POST, 1);
-
curl_setopt($client, CURLOPT_POSTFIELDS,$mypostdata);
-
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
-
$response = curl_exec($client);
-
$result = json_decode($response);
-
$mymessage= $result->status_message;
-
$myresult= $result->result;
-
echo "my message is ".$mymessage;
-
echo "my result is ".$myresult;
-
?>
最后是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>
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 blockfor 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:
Feel free to join our @steem-ua Discord server