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'}},
    });
});
JWDev
  • 856
  • 1
  • 10
  • 22
  • 2
    `$raw_post` looks like JSON, php won't parse that for you. If you send it like a url-encoded string it will. – Halcyon Nov 19 '15 at 14:28
  • 3
    Possible duplicate of [Angular HTTP post to PHP and undefined](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) – viral Nov 19 '15 at 14:29
  • Try manually trigger your PHP script with POST request via: Postman Launcher or similar add-on. – divix Nov 19 '15 at 14:31
  • [Here](http://stackoverflow.com/a/24140930/3113793) is what you need to do – viral Nov 19 '15 at 14:35
  • Thanks Halcyon, that works for setting the data type now.. but see my comment to Hanky's answer if you could. – JWDev Nov 19 '15 at 15:03
  • can you post your query here with header ? – Maxence Apr 20 '18 at 15:29

2 Answers2

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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • 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? – JWDev Nov 19 '15 at 15:00
  • What does $raw_input contain now? – Hanky Panky Nov 20 '15 at 20:10
0

Try like this,

print_r(json_decode($raw_post));

Looks like you have json data.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41