首页  >  文章  >  后端开发  >  php post json参数的传递和接收处理方法

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

不言
不言原创
2018-05-31 16:43:553290浏览

这篇文章主要介绍了关于php post json参数的传递和接收处理方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

页面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( 
   &#39;Content-Type: application/json; charset=utf-8&#39;, 
   &#39;Content-Length: &#39; . 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(&#39;a&#39;=>"weqweqwe", &#39;b&#39;=>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 = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;; 
var_dump(json_decode($json,true)); 
  
?>

页面2,参数接收处理:

2.php

<? 
$postData = file_get_contents(&#39;php://input&#39;); 
echo $postData; 
$data = json_encode(array(&#39;a&#39;=>" 234 ", &#39;b&#39;=>2)); 
echo $data; 
?>

以上就是本篇文章的全部内容了,感谢大家阅读。更多请关注PHP中文网!

相关推荐:

PHP给前端返回一个JSON对象

JSON PHP中,Json字符串反序列化成对象/数组的方法

以上是php post json参数的传递和接收处理方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn