首页 文章

使用PHP接收JSON POST

提问于
浏览
206

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

当我打印:

echo $_POST;

我明白了:

Array

我尝试这个时什么也没收到:

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

我尝试这个时什么也没收到:

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

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

8 回答

  • 52

    阅读文档:

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

    php Manual

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

    这对我有用 .

  • 390

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

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

    这将节省您将所有$ _POST更改为其他内容的麻烦,并允许您仍然发出正常的帖子请求,如果您想要删除此行 .

  • 21

    使用 $HTTP_RAW_POST_DATA 而不是 $_POST .

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

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

  • 11

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

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

    • 检查引号是否由于魔术引号标志而未被转义 .

    如果这两个检查无法解决您的问题,那么为了找出数据是什么样的,您应该在解码之前和之后回显或打印您的数据,以了解数据的接收和处理方式 .

  • -2

    值得指出的是,如果您使用 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);
    

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

  • 34

    尝试;

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

    从你的json和你的代码中,看起来你已经正确拼写了单词 operation ,但它不在json中 .

    EDIT

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

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

    我想发布一个也使用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)
    

相关问题