【问题标题】:How can use I use PHP as back-end for ionic framework? [closed]如何使用 PHP 作为 ionic 框架的后端? [关闭]
【发布时间】:2015-06-22 08:11:06
【问题描述】:

谁能给出一个在 Ionic 框架中在后端使用 php 并在前端使用 Angular JS 的示例?

【问题讨论】:

  • 是的,你可以很好地使用 php,但如果你已经在前端使用基于 js 的框架,为什么不对后端也这样做。您可以使用 Node.js + Express.js 或sails.js 与 mysql 数据库进行对象映射。它为实时场景提供了更好的性能,并且学习方法小而快

标签: php angularjs ionic-framework backend


【解决方案1】:

当然!

我和我的伙伴刚刚完成了一个集成了 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

【讨论】:

  • 是的。实际上我已经开发了一个网络应用程序。我一直在为此开发 android 和 ios 应用程序。所以我需要使用相同的数据库,即 mysql 和 xampp。所以我的问题是关于这个。
  • 我建议您使用 $http.jsonp 请求,因为对于基于 Cordova 的应用程序,CORS 是一个主要问题。在这种情况下,您需要在向 PHP 函数发送请求并从同一 PHP 获取响应时添加 callback 参数。所以你应该尝试一下,如果它不起作用,你可以发布你的代码,我可以帮助你。
  • 好吧。谢谢凯瓦尔。我会试试的。
猜你喜欢
  • 1970-01-01
  • 2018-10-15
  • 2015-07-12
  • 2020-11-27
  • 2013-05-19
  • 2013-10-18
  • 2010-12-23
  • 2012-07-10
  • 2016-12-03
相关资源
最近更新 更多