Home >Backend Development >PHP Tutorial >What is CURL in php? Code example for CURL in php
The content of this article is about what is CURL in php? The code example of CURL in php has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. What is CURL
CURL is a powerful library that allows us to connect and communicate with various servers using various types of protocols. Supports http, https, ftp, gopher, telnet, dict, file and ldap protocols. It also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (this can also be completed through PHP's FTP extension), HTTP form-based upload, proxy, cookies and username and password authentication.
2. The basic process of using CURL to send requests
curl_init() Initialize the connection handle;
curl_setopt() settings CURL options;
curl_exec() executes the request;
curl_close() releases the VURL connection handle.
3. Use CURL to send get/post requests
<?php /** *当$data为null时是get方式,否则为post方式 */ public function phpCurl($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } //get方式。注意:这里的$url已经包含参数了,不带参数你自己处理哦GET很简单 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, false); // 跳过证书检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在 $tmpInfo = curl_exec($curl); //返回api的json对象 //关闭URL请求 curl_close($curl); return $tmpInfo; //返回json对象 } /* PHP CURL HTTPS POST */ 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, 1); // 从证书中检查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格式 } ?>
The above is the detailed content of What is CURL in php? Code example for CURL in php. For more information, please follow other related articles on the PHP Chinese website!