Store data received from third parts in endpoint URL with PHP

Viewed 890

I'm trying to receive a data response from a third party API system to my URL endpoint with PHP. So the third parts is sending a post request to my endpoint(the url that I filled in in their API panel) whenever they have a new data response for me. Basically they are sending only the last response data when from their system they have a new submission. I have added this on my endpoint URL to see if I can see the data response received:

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

var_dump($request_data);

On their panel I can test the endpoint url result and on their panel I have this result when I click on the test request button, which is basically the result of my var_dump:

result in their API panel

But on my side the output is like a full string but I don't get nothing on my side at my end point URL.

my endpoint URL

This is the post_max_size within my phpinfo file so my the $_POST variable shouldn't be empty:

enter image description here

The format of the submitted data that I should expect to receive to my URL endpoint is like below:

{
    "data": {
        "id": "",
        "name": "",
        "email": "",
        "phone": "",
        "description": "",
        "street": "",
        "housenumber": "",
        "postcode": "",
        "city": "",
        "questions": {
            "Type opdracht": "",
            "Wat wil je laten opstellen?": ""
        },
        "questions_unmapped": {
            "114": "",
            "187": ""
        },
        "date": "",
        "notes": ""
    }
}

Is there any way to:

  • convert this string in a PHP object
  • print/store those data on my side (my endpoint URL)
4 Answers

Try $_POST instance of file_get_contents('php://input');

I emulated your 3rd Party APIs posting with a Curl script as follows

$data_string = '{
"operacion": {
   "tok": "[generated token]",
   "shop_id": "12313",
   "respuesta": "S",
   "respuesta_details": "respuesta S",
   "extended_respuesta_description": "respuesta extendida",
   "moneda": "PYG",
   "monto": "10100.00",
   "authorization_number": "123456",
   "ticket_number": "123456789123456",
   "response_code": "00",
   "response_description": "Transacción aprobada.",
   "security_information": {
       "customer_ip": "123.123.123.123",
       "card_source": "I",
       "card_country": "Croacia",
       "version": "0.3",
       "risk_index": "0"
   }
}}';


$ch = curl_init('http://localhost/test/index.php');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);
echo $result;

In my index.php I emulated your URL endpoint as follows

$request_data = file_get_contents('php://input');   
$decoded_params = json_decode($request_data,true);
var_dump($decoded_params);

And the output which I got is this.

Array
(
[operacion] => Array
    (
        [tok] => [generated token]
        [shop_id] => 12313
        [respuesta] => S
        [respuesta_details] => respuesta S
        [extended_respuesta_description] => respuesta extendida
        [moneda] => PYG
        [monto] => 10100.00
        [authorization_number] => 123456
        [ticket_number] => 123456789123456
        [response_code] => 00
        [response_description] => Transacción aprobada.
        [security_information] => Array
            (
                [customer_ip] => 123.123.123.123
                [card_source] => I
                [card_country] => Croacia
                [version] => 0.3
                [risk_index] => 0
            )

    )

)

I just made the following change to your endpoint code

$decoded_params = json_decode($request_data); => $decoded_params = json_decode($request_data,true);

Brother Ugol,

What I will do is, whenever someone send post request to our API(endpoint)

Solutions

Method 1

  1. Save the json into your database's tbl_input_json
  2. Then, when admin access to the system, they could decide whether they want to insert those json record permanently into database's tables or not.

Method 2

  1. Directly, use json_decode to change the string into php array
  2. Then, directly insert all these records into the database.

** Hope this solve your curiosity.

Unless you are receiving the request with a Content-Type: multipart/form-data, you should be able to get the body of the request using:

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

Now, if you are receiving a multipart/form-data request and you can't modify the content-type of its body, you can try changing the following option in your php.ini (PHP 5.4+):

enable_post_data_reading = On

This will allow you to parse POST requests using php://input BUT, it will disable the automatic parsing of data on PHP internal arrays $_POST and even $_FILES. See more information about this option on the following page: https://www.php.net/manual/en/ini.core.php#ini.enable-post-data-reading

You can read why you can't ready the body of the request for multipart/form-data on the following page (look for: php://input): https://www.php.net/manual/en/wrappers.php.php

After receiving the request, you can parse it in the following way (assuming is a JSON string, just like it looks like in the image you provided):

$jsonRequest = json_decode($requestBody, true);

json_decode documentation: https://www.php.net/manual/es/function.json-decode.php

Related