Alphonce
Alphonce

Reputation: 1

file_get_contents ('php://input') to get response from local server

am try to simulate Dajara mpesa api using my localhost server which i have managed to register the urls.Am logging the response to a text file call mpesa_confirmation.txt so that i can access the raw data from the server response.This the code am using.

<?php
header("Content-Type: application/json");

$data = file_get_contents('php://input');

$json_decode = json_decode($data);

//Grab inputs from the response
<?php
header("Content-Type: application/json");

$amount_recieved = $data['TransAmount'];//amount sent by the customer
$amount_expected = 150;//amount to be paid
if ($amount_recieved == $amount_expected) {

    //you can store data in the database
    //Then do other logics like proceed to next page

    $response = '{
    "ResultCode":0,
    "ResultDesc":"Payment recieved successfully"
}';

$json_response = json_encode($response);
echo $json_response;

} else {
    $response = '{
    "ResultCode":1,
    "ResultDesc":"Payment failed"
}';
$json_response = json_encode($response);
echo $json_response;
}

?>
<?php
// my confirmation file
header("Content-Type: application/json");

$data = file_get_contents('php://input');

$json_decode = json_decode($data);
$handle = fopen('../logs/Mpesa_confirmation.txt', 'w');
fwrite($handle, $data);
fclose($handle);


?>

This is what I am getting:

Warning: Illegal string offset 'TransAmount' in C:\xampp\htdocs\mqpesa\qpesa\validation\index.php on line 12

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\mqpesa\qpesa\validation\index.php on line 12 "{\r\n \"ResultCode\":1,\r\n \"ResultDesc\":\"Payment failed\"\r\n}"

Upvotes: 0

Views: 347

Answers (1)

ADyson
ADyson

Reputation: 61984

This code has a number of problems, some of which are directly related to your error, and some which are obvious from the code and could cause other issues:

1)

amount_recieved = $data['TransAmount'];

is incorrect. $data is the raw JSON string. You decoded that data into a PHP object using json_decode(). Therefore if you want to access a property, you need to use the decoded object, not the raw string:

amount_recieved = $json_decode['TransAmount'];

(N.B. Consider using a more accurate and meaningful variable name than $json_decode.)

2)

header("Content-Type: application/json");

is written twice. There's no need to set the header more than once in the same script. Set it once at the top, not repeatedly. However it's unclear if these code snippets are separate scripts or the same scripts, whether some of then "include()" the others, or how they are otherwise linked. If you expect to use $data in the second <?php block then you must have read it from the php input somewhere in the same script, or a script previously included in that script.

3)

$response = '{
  "ResultCode":0,
  "ResultDesc":"Payment recieved successfully"
}';
$json_response = json_encode($response);

This ends up double-encoding your data. json_encode() is intended to encode an object, not a string. JSON is a string format. The process of encoding is to turn an object into a string. Turning a string into a different string makes no sense. And don't write the JSON by hand - you just run the risk of unexpected syntax errors. Do it like this instead:

$response =  array("ResultCode" => 0, "ResultDesc" => "Payment recieved successfully");
$json_response = json_encode($response);

And then do the same for the second, near-identical example below it.

Upvotes: 1

Related Questions