使用PHP接收JSON POST

时间:2013-09-18 07:47:45

标签: php json post

我正在尝试在付款界面网站上收到JSON POST,但我无法对其进行解码。

我打印时:

echo $_POST;

我明白了:

Array

当我尝试这个时,我什么都没得到:

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

当我尝试这个时,我什么都没得到:

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

当我尝试这个时我得到NULL:

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

当我这样做时:

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

我明白了:

NULL

JSON格式(根据支付网站文档):

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

付款网站日志说一切正常。有什么问题?

10 个答案:

答案 0 :(得分:419)

尝试;

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

从你的json和你的代码中,看起来你已经正确拼写了操作这个词,但它不在json中。

修改

也许还值得尝试回应来自php:// input的json字符串。

echo file_get_contents('php://input');

答案 1 :(得分:68)

如果您已将参数设置为$ _POST ['eg'],并且您不想更改它,只需这样做:

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

这样可以省去将所有$ _POST更改为其他内容的麻烦,并且如果您希望将此行删除,您仍可以发出正常的帖子请求。

答案 2 :(得分:37)

使用$HTTP_RAW_POST_DATA代替$_POST

它将按原样为您提供POST数据。

您稍后可以使用json_decode()对其进行解码。

答案 3 :(得分:28)

值得指出的是,如果您使用json_decode(file_get_contents("php://input"))(正如其他人提到的那样),如果字符串不是有效的JSON,则会失败。

首先检查JSON是否有效,可以简单地解决这个问题。即。

function isValidJSON($str) {
   json_decode($str);
   return json_last_error() == JSON_ERROR_NONE;
}

$json_params = file_get_contents("php://input");

if (strlen($json_params) > 0 && isValidJSON($json_params))
  $decoded_params = json_decode($json_params);

修改:请注意,删除上面的strlen($json_params)可能会导致细微的错误,因为json_last_error()null 传递一个空字符串,如下所示: http://ideone.com/va3u8U

答案 4 :(得分:11)

阅读文档:

  

通常,应使用php://输入而不是$ HTTP_RAW_POST_DATA。

php Manual

一样

答案 5 :(得分:7)

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

这对我有用。

答案 6 :(得分:2)

很晚。
好像(OP)已经尝试了所有给他的答案。
即使您(OP)没有收到传递给“ .PHP”文件的内容,也可能是错误的URL错误。
检查您是否在调用正确的“ .PHP”文件。
(URL中的拼写错误或大写字母)
最重要的
检查您的URL在“ http”之后是否有“ s”(安全)。
示例:

....
a = soup.find_all(class_='sg-col-16-of-20 sg-col sg-col-8-of-12 sg-col-12-of-16')
price = [item.find(class_='a-price-whole') for item in a]
print(price)
....

应该是

res = adv_model.predict(x={'feature': x_test, 'label': y_test})

或任何一种方式。
添加或删除“ s”以匹配您的URL。

答案 7 :(得分:-1)

使用php://输入可能有所帮助,但它对我没有用,这是做了什么:

  • 检查数据的编码。如果是URLEncoded,请确保它已发送URLEncoded。

  • 检查引号是否因魔法引号标记而被转义。

如果这两个检查没有解决您的问题,那么为了找出数据是什么样的,您应该在解码之前和之后回显或打印您的数据,以了解数据的接收方式并加工。

答案 8 :(得分:-1)

你可以像下面这样使用.. 像下面这样发布JSON

enter image description here

从php项目用户获取数据如下

// takes raw data from the request 
$json = file_get_contents('php://input');
// Converts it into a PHP object 
$data = json_decode($json, true);

 echo $data['requestCode'];
 echo $data['mobileNo'];
 echo $data['password'];

答案 9 :(得分:-2)

我想发布一个答案,该答案也使用curl来获取内容,并使用mpdf来将结果保存为pdf,这样您就可以得到典型用例的所有步骤。只是原始代码(以便适应您的需求),但是它可以工作。

// import mpdf somewhere
require_once dirname(__FILE__) . '/mpdf/vendor/autoload.php';

// get mpdf instance
$mpdf = new \Mpdf\Mpdf();

// src php file
$mysrcfile = 'http://www.somesite.com/somedir/mysrcfile.php';
// where we want to save the pdf
$mydestination = 'http://www.somesite.com/somedir/mypdffile.pdf';

// encode $_POST data to json
$json = json_encode($_POST);

// init curl > pass the url of the php file we want to pass 
// data to and then print out to pdf
$ch = curl_init($mysrcfile);

// tell not to echo the results
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );

// set the proper headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($json) ]);

// pass the json data to $mysrcfile
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

// exec curl and save results
$html = curl_exec($ch);

curl_close($ch);

// parse html and then save to a pdf file
$mpdf->WriteHTML($html);
$this->mpdf->Output($mydestination, \Mpdf\Output\Destination::FILE);

在$ mysrcfile中,我将像这样读取json数据(如先前的回答所述):

$data = json_decode(file_get_contents('php://input'));
// (then process it and build the page source)