Home  >  Article  >  Backend Development  >  How does php curl use post to send data? post method to send data

How does php curl use post to send data? post method to send data

不言
不言Original
2018-08-08 15:45:541716browse

The content of this article is about how php curl uses post to send data? The post method of sending data has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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.

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.

Recommended related articles:

What are the functions of php curl? Application of php curl library (with code)

PHP7 source code: Detailed analysis of PHP virtual machine

The above is the detailed content of How does php curl use post to send data? post method to send data. 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