欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」json参数的传递和接收处理方法

作者 : 自学哈 本文共1261个字,预计阅读时间需要4分钟 2022-10-8 共171人阅读
也想出现在这里? 联系我们

php传递json参数示例

<?  
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));  
   
?>

接收json处理示例

<?  
$postData = file_get_contents('php://input');  
echo $postData;  
$data = json_encode(array('a'=>" 234 ", 'b'=>2));  
echo $data;  
?>
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」json参数的传递和接收处理方法
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号