如何使用 PHP 作为离子框架的后端?

How can use I use PHP as back-end for ionic framework?

谁能举例说明在 Ionic Framework 中后端使用 php 而前端使用 Angular JS?

当然可以!

我和我的合作伙伴刚刚完成了一个集成了 PHP 作为后端的 IONIC 应用程序的工作。

就像常规的前端-后端一样,请求和响应采用 JSON.

的形式

为了快速入门,这里是我们为自己构建的示例代码:

send.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" => "ifyoucanreadthis@yes.com",
    "age"  => 22
    );

// Send the data.
echo json_encode($data);
?>

recieve.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.
    */    

?>

希望对您有所帮助!

编辑 1:

使用 PDO 的好处被高估了。在这里阅读更多相关信息:http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

我假设您了解连接到数据库的基本代码 (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";
?>

就 Angular 编码而言,您可能会发现以下链接很有用(抱歉,我的机器上没有 angular 代码):

http://codeforgeek.com/2014/07/angular-post-request-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