在Angular中使用PHP从$resource中获取参数值


Getting parameter value from $resource in Angular using PHP

我正在尝试使用Angular工厂从Wordpress数据库检索数据。使用ng-click="updateCommentsById(v.id.videoId)",我调用以下函数:

$scope.updateCommentsById = function(id) {
    commentRepository.query(({videoId: id}), function (data) {
        $scope.comments = data;
    });
}

对应于以下工厂定义:

angular.module("app")
    .factory("commentRepository",
        function($resource) {
            return $resource("/wp-content/themes/zerif-lite-child/inc/get_comments.php/:videoId",
            {
                videoId:"@id"
            });
        });

问题是如何获得videoId参数进入我的PHP函数内get_comments.php:

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-load.php");
function get_comments_by_id($id)
{
    echo $id;
    if (!is_user_logged_in()) {
        echo json_encode("Not Authorised");
    } else {
        global $wpdb;
        $result = $wpdb->get_results("SELECT * FROM analyser_posts WHERE video_id = $id", OBJECT);
        echo wp_json_encode($result);
    }
}
get_comments_by_id(videoId);

编辑:

原来get_results()方法不允许SQL语句中的变量,我应该使用prepare()(无论如何更安全)代替。我还更改了请求URL。新代码变成:

angular.module("app")
    .factory("commentRepository",
        function ($resource) {
            return $resource("/wp-content/themes/zerif-lite-child/inc/get_comments.php?video_id=:videoId");
        });
PHP:

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-load.php");
function get_comments_by_id($id)
{
    var_dump($id);
    if (!is_user_logged_in()) {
        echo json_encode("Not Authorised");
    } else {
        global $wpdb;
        $result = $wpdb->prepare("SELECT * FROM analyser_posts WHERE video_id = $id", OBJECT);
        var_dump($result);
        $result_array = array();
        if($result){
            foreach($result_array as $r){
                $result_array[] = $r;
            }
        }
        var_dump($result_array);
        echo json_encode($result_array);
    }
}
get_comments_by_id($_GET["video_id"]);

然而,var_dumps显示id得到了正确的传递,只有prepare()实际上没有执行任何操作。要不要用get_results来封装?

可以从URI中提取:

//$args is an array of every part separated by `/` (ignoring the query string)
$args = explode('/',strtok(trim($_SERVER['REQUEST_URI'],'/'),'?'));
//the last element is the video id
$video_id = end($args);

现场演示

使用答案(包括现在删除的)和评论使其工作。我的Angular控制器代码中的函数:

$scope.updateCommentsById = function(id) {
    commentRepository.query(({videoId: id}), function (data) {
        $scope.comments = data;
    });
}

存储库:

angular.module("app")
    .factory("commentRepository",
        function ($resource) {
            return $resource("/wp-content/themes/zerif-lite-child/inc/get_comments.php?video_id=:videoId");
        });

get_comments.php:

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-load.php");
function get_comments_by_id($id)
{
    if (!is_user_logged_in()) {
        echo json_encode("Not Authorised");
    } else {
        global $wpdb;
        $sql = $wpdb->prepare("SELECT * FROM `analyser_posts` WHERE `video_id` = '$id'", OBJECT);
        $result = $wpdb->get_results($sql);
        echo json_encode($result);
    }
}
get_comments_by_id($_GET["video_id"]);