How does one retrieve the params passed through a $http.get request using PHP?
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
$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. 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.
$http
do it for you – charlietfl Jan 11 at 23:51file_get_contents
? I guess PHP told you, that it expects astring
(file name) as the first parameter and you're passing anarray
instead ($_GET
). It doesn't make a sense. BTW as the name suggests, functionfile_get_contents
returns you a content of file specified by the filename in the first parameter as astring
. But you're just sending a GET parameter, there are no files at all, why would you like to use that function? You already get the GET parameter as a string in the$_GET
array. – Dawid Ferenczy Jan 12 at 0:56file_get_contents("php://input")
at first because that's what I'm doing for mypost
request and it works. I wasn't exactly sure what I was doing at this point. – Bryner Jan 12 at 4:45