简体   繁体   English

PHP-从AngularJS $ http.get请求获取参数

[英]PHP - get params from AngularJS $http.get request

How does one retrieve the params passed through a $http.get request using PHP? 如何使用PHP检索通过$ http.get请求传递的参数?

This is my controller: 这是我的控制器:

app.controller('phonesCtrl', function ($scope, $http, $routeParams) {

    $scope.make = $routeParams.make;
    console.log($routeParams); // Console displays 'Object {make: "apple"}'
    console.log($scope.make); // Console displays 'apple'

    $http({
        method: 'POST',
        url: 'tools/get.php',
        params: $routeParams, // Already an object: "{"make" : apple}"
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
    .then(function (response) {
        $scope.phones = response.data;
    });
});

My PHP attempt: works with $http.post, but something tells me I can't use the file_get_contents function to do the same. 我的PHP尝试:与$ http.post一起使用,但是有什么告诉我我不能使用file_get_contents函数来执行相同操作。

<?php

$data = json_decode(file_get_contents("php://input"));

var_dump($data); // Outputs NULL
var_dump($_POST["make"]); // Outputs NULL
var_dump($_POST); // Outputs empty array

$con = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'root');

$stmt = $con->prepare("SELECT * FROM phones WHERE make = ?");
$stmt->bindParam(1, $make);
$stmt->execute();

$result = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));

print_r($result);

?>

I'm also using the $routeProvider service to keep my links clean. 我还使用$routeProvider服务保持链接清洁。 At first I realized the console was outputting blank objects so I changed my config to: 起初,我意识到控制台正在输出空白对象,因此我将配置更改为:

.when('/get/:make', { // Added /:make
    templateUrl: 'tools/get.php',
    controller: 'phonesCtrl'
})

So now my console outputs "apple" when I navigate to /get/apple. 因此,现在当我导航到/ get / apple时,控制台将输出“ apple”。

In PHP use 在PHP中使用

$_GET["make"]

To get the params. 获得参数。

If you read PHP $_POST documentation here you can find 如果您在此处阅读PHP $ _POST文档,则可以找到

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded [what you are using] or multipart/form-data as the HTTP Content-Type in the request. 当将application / x-www-form-urlencoded [您正在使用的内容]或multipart / form-data用作请求中的HTTP Content-Type时,通过HTTP POST方法传递给当前脚本的变量的关联数组。

What you are trying to do is to pass a GET request with POST parameters. 您尝试做的是传递带有POST参数的GET请求。 You ether pass it like this www.yoursite.it/page.php?name=Matteo and then you take the variable with $name = $_GET['name'] or you pass in your way but in POST so like this: 您可以像这样通过www.yoursite.it/page.php?name=Matteo传递它,然后使用$name = $_GET['name']接受变量,或者以POST方式传递,如下所示:

$http({
method: 'POST', //CHANGE THIS FROM GET TO POST
url: 'tools/get.php',
params: {
    name: 'Matteo' //USE PROPER JAVASRIPT OBJECTS
},
headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
}
})

And then you get the variable with $name = $_POST['name'] 然后,您将获得带有$name = $_POST['name']的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM