向指定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)."\n";
    }
}
var_dump($response);die;

接收方代码:

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

 

posted on 2014-05-15 17:45  糖果不吃糖  阅读(871)  评论(0编辑  收藏  举报

导航