Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive JSON POST with PHP

Tags:

json

post

php

I’m trying to receive a JSON POST on a payment interface website, but I can’t decode it.

When I print :

echo $_POST; 

I get:

Array 

I get nothing when I try this:

if ( $_POST ) {     foreach ( $_POST as $key => $value ) {         echo "llave: ".$key."- Valor:".$value."<br />";     } } 

I get nothing when I try this:

$string = $_POST['operation']; $var = json_decode($string); echo $var; 

I get NULL when I try this:

$data = json_decode( file_get_contents('php://input') ); var_dump( $data->operation ); 

When I do:

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

I get:

NULL 

The JSON format is (according to payment site documentation):

{    "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"        }     } } 

The payment site log says everything is OK. What’s the problem?

like image 211
Pablo Ramirez Avatar asked Sep 18 '13 07:09

Pablo Ramirez


People also ask

How get post JSON in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How pass JSON object in post request in PHP?

Send JSON data via POST with PHP cURLInitiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.

How to parse JSON in PHP?

First, you need to get the data from the file into a variable by using file_get_contents() . Once the data is in a string, you can call the json_decode() function to extract information from the string. Keep in mind that JSON simply provides a way to store information as a string using a set of predefined rules.


1 Answers

Try;

$data = json_decode(file_get_contents('php://input'), true); print_r($data); echo $data["operacion"]; 

From your json and your code, it looks like you have spelled the word operation correctly on your end, but it isn't in the json.

EDIT

Maybe also worth trying to echo the json string from php://input.

echo file_get_contents('php://input'); 
like image 191
Dom Avatar answered Sep 18 '22 15:09

Dom