Home >Backend Development >PHP Tutorial >PHP post json parameter transmission and reception processing method

PHP post json parameter transmission and reception processing method

不言
不言Original
2018-05-31 16:43:553428browse

This article mainly introduces the transmission and reception processing methods of php post json parameters. It has certain reference value. Now I share it with everyone. Friends in need can refer to

Page 1, php transmission json parameter page:

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

Page 2, parameter reception Processing:

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

The above is the entire content of this article Well, thank you all for reading. Please pay attention to the PHP Chinese website for more information!

Related recommendations:

PHP returns a JSON object to the front end

JSON In PHP, the Json string is deserialized into Methods of objects/arrays

The above is the detailed content of PHP post json parameter transmission and reception processing method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn