当前位置: 移动技术网 > IT编程>开发语言>PHP > php post json参数的传递和接收处理方法

php post json参数的传递和接收处理方法

2018年08月10日  | 移动技术网IT编程  | 我要评论

页面1 ,php传递json参数的页面:

1.php

<? 
function http_post_data($url, $data_string) { 
 
  $ch = curl_init(); 
  curl_setopt($ch, curlopt_post, 1); 
  curl_setopt($ch, curlopt_url, $url); 
  curl_setopt($ch, curlopt_postfields, $data_string); 
  curl_setopt($ch, curlopt_httpheader, array( 
   'content-type: application/json; charset=utf-8', 
   'content-length: ' . strlen($data_string)) 
  ); 
  ob_start(); 
  curl_exec($ch); 
  $return_content = ob_get_contents(); 
  //echo $return_content."<br>"; 
  ob_end_clean(); 
 
  $return_code = curl_getinfo($ch, curlinfo_http_code); 
  // return array($return_code, $return_content); 
  return $return_content; 
 } 
 
$url = "http://127.0.0.1/2.php"; 
$data = json_encode(array('a'=>"weqweqwe", 'b'=>2));  
 
//list($return_code, $return_content) = http_post_data($url, $data); 
$aaa = http_post_data($url, $data); 
//print_r($aaa); 
echo $aaa; 
 
$ccc=json_decode($aaa); 
print_r($ccc); 
echo $ccc->b; 
 
echo "<hr>"; 
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 
var_dump(json_decode($json,true)); 
  
?> 

页面2,参数接收处理:

2.php

<? 
$postdata = file_get_contents('php://input'); 
echo $postdata; 
$data = json_encode(array('a'=>" 234 ", 'b'=>2)); 
echo $data; 
?> 

以上这篇php post json参数的传递和接收处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网