首页 > 解决方案 > 如何在 PHP 中获取发送的 json POST cURL

问题描述

我正在尝试使用 post 访问从sender.php发送到receiver.php的数据。

这是我的sender.php

<?php
 $param = array(
        'Name' => 'Mark',
        'Age' => '28'
    );
    $ch = curl_init();

    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://example/receiver.php',
        CURLOPT_POST => 'TRUE',
        CURLOPT_HEADER => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Content-Length: '.strlen(json_encode($param))
        ),
        CURLOPT_POSTFIELDS => json_encode($param),
        )
    );

$result = curl_exec($ch);
curl_close($ch);
print_r($result);


?>

这是我的receiver.php。我需要有关如何显示作为结果输入的数据的帮助

<?php
echo json_decode(file_get_contents('https://example/sender.php'));
?>

标签: phpjsoncurlpostget

解决方案


推荐阅读