Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
you could try to put a javascript object instead of a string as a parameter to $http: var make = {make: $routeParams.phone}; – Patrick Kelleter Jan 11 at 23:37
    
never try making json manually ... it is error prone and much simpler to let $http do it for you – charlietfl Jan 11 at 23:51
    
Why do you ask about file_get_contents? I guess PHP told you, that it expects a string (file name) as the first parameter and you're passing an array instead ($_GET). It doesn't make a sense. BTW as the name suggests, function file_get_contents returns you a content of file specified by the filename in the first parameter as a string. 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:56
    
@DawidFerenczy I was just playing around at this point. I had file_get_contents("php://input") at first because that's what I'm doing for my post request and it works. I wasn't exactly sure what I was doing at this point. – Bryner Jan 12 at 4:45
    
@PatrickKelleter Thank you for pointing that out, I was thinking it was a lot more difficult than it had to be. – Bryner Jan 12 at 4:46

If you read PHP $_POST documentation here you can find

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.

What you are trying to do is to pass a GET request with POST parameters. 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:

$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']

share|improve this answer
    
I've updated the question to include the $routeProvider.config and corrected the URL for it to include /:param. I was getting an empty object prior to adding that. I've also attempted to pass the params as a post and retrieve them with $_POST and I've also tried to pass the params through the url tools/get.php?make=apple and tried to retrieve them using $_GET and I still output NULL or empty array. What could I be missing? – Bryner Jan 12 at 5:29
    
Can you update your answer with the new code? – Smile Applications Jan 12 at 6:33
    
It's been updated. – Bryner Jan 12 at 6:51
    
Can you see in your dev console if the params are passed in this way: make=Apple in the form data panel ? Maybe post a screenshot – Smile Applications Jan 12 at 7:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.