首页 / PHP教程 / php post json参数的传递和接收处理方法

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

今天小编就为大家分享一篇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(  
   '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;  
?>

本文来自网络,不代表糖果站长网立场,转载请注明出处:https://www.1tg.com.cn/html/jc/php/2021/1114/19071.html

dawei作者

【声明】:糖果站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
上一篇
下一篇

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部

【免责声明】本站内容转载自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请提交相关链接至邮箱xwei067@foxmail.com我们将及时予以处理。

Copygight © 2013-2023 https://www.1tg.com.cn All Rights Reserved.糖果站长网

站长:xwei067#foxmail.com(请把#换成@)