3

In PHP I have a form that submits data to a PHP script. The PHP script prints the values, using:

  $raw_post = file_get_contents('php://input');
  print_r($raw_post);
  print_r($_POST);
  print_r($_REQUEST);

All of these come back empty/null arrays EXCEPT $raw_post (aka, php://input).

Chrome's Developer Tools also show that the values have been submitted through the payload as a POST request and it is a status of 200 OK, yet PHP does not set them to the $_POST array at all.

Results in the $raw_post:

{"company_name":"test","primary_contact":"test","address":"test","function":"test","phone":"test","fax":"test","url":"test"}

Results in $_POST:

Array
(
)

Results in $_REQUEST:

Array
(
)

I am unable to find a solution to this issue ... could anybody help here?

The form is submitted from AngularJS to a PHP script.

New code (url-encoded):

app.factory('Companies', function($resource) {
    return $resource('/api.php/companies/:id', {id:''}, {
        'query': {method: 'GET', isArray: true},
        'view': {method: 'GET', isArray: true},
        'save': {
            method: 'POST',
            isArray: true,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}},
    });
});
6
  • 2
    $raw_post looks like JSON, php won't parse that for you. If you send it like a url-encoded string it will. Commented Nov 19, 2015 at 14:28
  • 3
    Possible duplicate of Angular HTTP post to PHP and undefined Commented Nov 19, 2015 at 14:29
  • Try manually trigger your PHP script with POST request via: Postman Launcher or similar add-on. Commented Nov 19, 2015 at 14:31
  • Here is what you need to do Commented Nov 19, 2015 at 14:35
  • Thanks Halcyon, that works for setting the data type now.. but see my comment to Hanky's answer if you could. Commented Nov 19, 2015 at 15:03

2 Answers 2

1

Wow that sounds pretty simple doesnt it

$raw_post = file_get_contents('php://input');
print_r($raw_post); 

That already gives you the Posted JSON, just decode it :)

$values=json_decode($raw_post,true);

Now if you wanted to store all this data back in $_POST, you can simply do

$_POST=json_decode($raw_post,true);

That gives you your posted data.

Output

Array
(
    [company_name] => test
    [primary_contact] => test
    [address] => test
    [function] => test
    [phone] => test
    [fax] => test
    [url] => test
)

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Added some new code to OP - the data is now url-encoded via AngularJS, however, the $_POST is still empty. I think this is because chrome is saying the method is still application/json, whereas the data is now url-encoded. Any ideas?
What does $raw_input contain now?
0

Try like this,

print_r(json_decode($raw_post));

Looks like you have json data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.