向指定URL发送JSON数据

习惯了使用CURL的方式,发送GET 和POST 请求

今天需要使用POST 发送JSON数据的

发送方代码:

$data = '{"sign":"111","data":{"failedDesc":"","amount":"50.00","callbackInfo":"sssss"}}';

$url = 'http://127.0.0.1/dev/Test/url.php';
// performs the HTTP POST
$opts = array ('http' => array (
    'method'  => 'POST',
    'header'  => 'Content-type: application/json',
    'content' => $data
));
$context  = stream_context_create($opts);
$response = '';
if ($fp = fopen($url, 'r', false, $context)) {
    while($row = fgets($fp)) {
        $response.= trim($row)."
";
    }
}
var_dump($response);die;

接收方代码:

echo file_get_contents('php://input');
原文地址:https://www.cnblogs.com/ouzhenzhou/p/3730519.html