如何使用 PHP 作为 ionic 框架的后端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29665933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can use I use PHP as back-end for ionic framework?
提问by Abhay Naik
Can anyone give an example of using php at the backend with Angular JS at the front-end in the Ionic Framework?
任何人都可以举个例子,在后端使用 php,在 Ionic 框架的前端使用 Angular JS?
回答by Shalabh
Of Course !
当然 !
Me and my partner just completed working on a IONIC App integrated with PHP as its backend.
我和我的伙伴刚刚完成了一个以 PHP 为后端的 IONIC 应用程序的开发工作。
Just like a regular Frontend-backend, requests and responses are in the form of JSON.
就像常规的前端-后端一样,请求和响应采用 JSON 的形式。
For getting started quickly, here is a sample code we built for ourselves :
为了快速入门,以下是我们为自己构建的示例代码:
send.php
发送.php
<?php
// Prevent caching.
//header('Cache-Control: no-cache, must-revalidate');
// The JSON standard MIME header.
//header('Content-type: application/json');
$data = array(
"username" => "one",
"email" => "[email protected]",
"age" => 22
);
// Send the data.
echo json_encode($data);
?>
recieve.php
接收.php
<?php
/*
* Collect all Details from Angular HTTP Request.
*/
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$usr = $request->email;
$pass = $request->pass;
echo "<h1> Username is : " . $usr . "<br /> and password is : ". $pass."</h1>"; //this will go back under "data" of angular call.
/*
* You can use $email and $pass for further work. Such as Database calls.
*/
?>
Hope this helps you !
希望这对你有帮助!
EDIT 1 :
编辑 1:
The benefits of using PDO is over-rated. Read more about it here : http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059
使用 PDO 的好处被高估了。在此处阅读更多相关信息:http: //code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059
I am assuming that you know about basic code for connecting to a database (http://www.w3schools.com/php/php_mysql_intro.asp).
我假设您了解连接到数据库的基本代码 ( http://www.w3schools.com/php/php_mysql_intro.asp)。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
As far as Angular coding is concerned, you may find the following links useful (sorry I don't have the angular code on this machine) :
就 Angular 编码而言,您可能会发现以下链接很有用(抱歉,我在这台机器上没有 Angular 代码):
http://codeforgeek.com/2014/07/angular-post-request-php/
http://codeforgeek.com/2014/07/angular-post-request-php/
http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/
http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/
http://serebrov.github.io/html/2013-05-24-angular-post-to-php.html
http://serebrov.github.io/html/2013-05-24-angular-post-to-php.html