Use post to send data
What if we want to send POST data? We need to use curl to help us send data.
Following the steps, we customized a function named: post. Two parameters need to be passed in to the post method:
1. The requested URL address
2. The data sent
The data sent are all arrays, with key values Just use the POST method to send the correct form to the specified interface address.
We only need to combine the "15.1 curl usage steps" to complete the corresponding code.
When developing a WeChat public account to create a custom menu, you need to use the POST method to send custom menu data to WeChat's custom menu interface.
Post's custom function, the entire code is as follows:
<?php function post($url, $data) { //初使化init方法 $ch = curl_init(); //指定URL curl_setopt($ch, CURLOPT_URL, $url); //设定请求后返回结果 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //声明使用POST方式来进行发送 curl_setopt($ch, CURLOPT_POST, 1); //发送什么数据呢 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //忽略证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //忽略header头信息 curl_setopt($ch, CURLOPT_HEADER, 0); //设置超时时间 curl_setopt($ch, CURLOPT_TIMEOUT, 10); //发送请求 $output = curl_exec($ch); //关闭curl curl_close($ch); //返回数据 return $output; } ?>
In the future, the WeChat public platform or other third-party API systems will be called. They need to use the POST method when asking you to send data.
When you need to use POST to send data, you only need to adjust the post method.