An example of PHP calling interface transmitting json data by post method

  • 2021-10-15 10:09:17
  • OfStack

First, the data is converted into json format, and then the interface is called through curl method and the parameters are passed

The code is as follows:


$keyword = urlencode($_POST['keyword']);
$parameters = json_encode(array('keyWord'=>$keyword,'areaCode'=>'*'));
$post_data['appToken'] = "323ds7674354fds32fdsda60173";// Written casually 
$post_data['parameters'] = $parameters;
$url = 'http://serde.com/compadddvd/index';// Written casually 
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);// Use post Method passes parameters 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

After that, the data can be returned.


Related articles: