Avinash February 2016

could not read data in post method in php

After successful payment, page post data to my server page. There i can only read data by

foreach($_POST as $key => $value)
  {
   file_put_contents($filename, "$key: $value\n", FILE_APPEND);        
  }

this php code and write to text file but do not get any data by normal post variable.

For example status key sent through post data if i called through $_POST['status'] i do not get any value through it.

so what happen here i am out of ideas. So please guide me here to get post data.

Documentation define data send in post method and encrypt type in application/x-www-form-urlencoded

My php file code Where post data redirect to

<?php
  session_start();


echo "post Data:"; print_r($_POST);   //Nothing Show


$filename = "webhook_data2.txt";

foreach($_POST as $key => $value)

{

    file_put_contents($filename, "$key: $value\n", FILE_APPEND);


}

file_put_contents($filename, "----------\n", FILE_APPEND);

?>

This works and out put of text file is as below :

buyer_name: xxxx xxxxx xxxxx
offer_slug: xxxx xxxxxx xxxxx
currency: INR
custom_fields: {"Field_42842": {"value": "xxxxx@xxxx.org", "required": "on", "type": "char", "label": "Email Mentioned on the Form"}, "Field_85492": {"value": "XXX XXX XXX", "required": "on", "type": "char", "label": "Student Name"}}
status: Credit
quantity: 1
variants: []
payment_id: MXXXXXXXXXXXX
buyer: xxxxx@xxxx.org
amount: xxxx.00
fees: 0.xx
buyer_phone: ############
offer_title: ###### #### #########
mac: ******************
unit_price: ##.00

Answers


Robert February 2016

Check if post is send

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = '';
    foreach($_POST as $key => $value) {
          $data .= "$key: $value" . PHP_EOL; 
    }
    //write data
    file_put_contents($filename, $data);  
} else {
    die('POST REQUEST IS NOT SEND');
}
  1. Getting data and then writing it to file once is faster than writing it in loop.
  2. If post is not request type(not send) then it will fail and it will display 'post request is not send'
  3. If 2 appears then check if your form method is set to POST and action url is correct.

Also if your POST data is raw then you can read it from stream named php://input with functions that read from stream for example

file_get_contents(`php://input`);

to read RAW post and save it you can use code below

file_put_contents($filename, file_get_contents(`php://input`));  

When your request is in RAW form for example JSON/XML it won't be mapped to $_POST automatically you need decode the input by your own for example if it is json then use json_decode, if it is xml then you can use DOM or simplexml. From manual:

$_POST

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.


isnisn February 2016

Actually sometimes $_POST is empty for various reasons. When that happens to me it always work to read php://input like $postdata = file_get_contents("php://input");

$postdata is now json-string that you can convert to an array like:

$arr = json_decode($postdata, true);

Then you should be able to do:

foreach($arr as $key => $value) { file_put_contents($filename, "$key: $value\n", FILE_APPEND);
}

Post Status

Asked in February 2016
Viewed 1,890 times
Voted 12
Answered 2 times

Search




Leave an answer