php curl uses the post method: first start a CURL session; then check the source of the authentication certificate; then check whether the SSL encryption algorithm exists from the certificate; and finally request the https protocol interface in POST mode.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
How to use the post method with php curl?
PHP: CURL requests the HTTPS/http protocol interface api in GET and POST modes respectively
curl requests the https protocol interface in GET mode
function curl_get_https($url){ $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 $tmpInfo = curl_exec($curl); //返回api的json对象 //关闭URL请求 curl_close($curl); return $tmpInfo; //返回json对象 }
curl requests https protocol interface in
POST
method
function curl_post_https($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $tmpInfo; // 返回数据,json格式 }
general encapsulation Interface
/** * CURL GET || post请求 * @desc: GET与post都通用 * @author: Sindsun * @email: 2361313833@qq.com * @date: 2019年4月24日上午10:54:31 * @param: $url 请求的地址 * $isPostRequest 默认true是GET请求,否则是POST请求 * $data array 请求的参数 * $certParam array ['cert_path'] ['key_path'] * @return: */ function curl_http($url, $isPostRequest=false, $data=[], $header=[], $certParam=[]){ // 模拟提交数据函数 $curlObj = curl_init(); // 启动一个CURL会话 //如果是POST请求 if( $isPostRequest ){ curl_setopt($curlObj, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curlObj, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包 }else{ //get请求检查是否拼接了参数,如果没有,检查$data是否有参数,有参数就进行拼接操作 $getParamStr = ''; if(!empty($data) && is_array($data)){ $tmpArr = []; foreach ($data as $k=>$v){ $tmpArr[] = $k . '=' . $v; } $getParamStr = implode('&', $tmpArr); } //检查链接中是否有参数 $url .= strpos($url, '?') !== false ? '&' . $getParamStr : '?' . $getParamStr; } curl_setopt($curlObj, CURLOPT_URL, $url); // 要访问的地址 //检查链接是否https请求 if(strpos($url, 'https') !== false){ //设置证书 if( !empty($certParam) && isset($certParam['cert_path']) && isset($certParam['key_path']) ){ curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 curl_setopt($curlObj, CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($curlObj, CURLOPT_SSLCERT, $certParam['cert_path']); curl_setopt($curlObj, CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($curlObj, CURLOPT_SSLKEY, $certParam['key_path']); }else{ curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 } } // 模拟用户使用的浏览器 if(isset($_SERVER['HTTP_USER_AGENT'])){ curl_setopt($curlObj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); } curl_setopt($curlObj, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curlObj, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curlObj, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curlObj, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curlObj, CURLOPT_HTTPHEADER, $header); //设置头部 curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $result = curl_exec($curlObj); // 执行操作 if ( curl_errno($curlObj) ) { $result = 'error: '.curl_error($curlObj);//捕抓异常 } curl_close($curlObj); // 关闭CURL会话 return $result; // 返回数据,json格式 }
Description: The premise is to turn on the curl switch of php and the ssl_module of the server, otherwise it cannot be used normally.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use post method in php curl. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft