How to use post method in php curl
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
