如何使用PHP作为离子框架的后端? [英] How can use I use PHP as back-end for ionic framework?

查看:149
本文介绍了如何使用PHP作为离子框架的后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在Ionic Framework的前端使用Angular JS在后端使用php吗?

Can anyone give an example of using php at the backend with Angular JS at the front-end in the Ionic Framework?

推荐答案

当然!

我和我的合作伙伴刚刚完成了与PHP集成的IONIC App的后端工作。

Me and my partner just completed working on a IONIC App integrated with PHP as its backend.

就像常规前端后端一样,请求和响应采用JSON格式。

Just like a regular Frontend-backend, requests and responses are in the form of JSON.

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

For getting started quickly, here is a sample code we built for ourselves :

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

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

我假设你知道连接数据库的基本代码( http://www.w3schools.com/php /php_mysql_intro.asp )。

I am assuming that you know about basic code for connecting to a database (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编码而言,您可能会发现以下链接有用(对不起,我不知道)在这台机器上有角度代码):

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) :

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

这篇关于如何使用PHP作为离子框架的后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆