Home >Backend Development >PHP Tutorial >Basic usage of curl function extension in PHP
PHP’s curl function provides many functions, which need to be combined together in specific steps. Let’s first understand the basic steps of setting up a curl request in PHP.
$ch = curl_init(); // 创建一个新的CURL资源赋给变量$ch curl_setopt($ch, CURLOPT_URL, $url); // 设置URL $response = curl_exec($ch); // 执行,获取URL并输出到浏览器 curl_close($ch); // 释放资源
If we want to get the content but not output it, we can use the CURLOPT_RETURNTRANSFER parameter and set its value to a non-0 or true value.
The code is as follows:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
We can get different results by setting different parameters of the function curl_setopt(). This is also the power of the CURL extension. The common parameter options of the curl_setopt() function can be found in the official documentation and will not be listed here.
The following are my commonly used curl get and post request methods:
Get request:
public function httpGet(string $url = '') { // 记录请求信息的日志 // todo try { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); //https 请求 if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $response = curl_exec($ch); $errorCode = curl_errno($ch); curl_close($ch); if (!empty($errorCode)) { // 可记录错误码日志 return null; } // 记录返回结果日志 return $response; } catch (\Exception $e) { $errorLog = [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'data' => [ 'url' => $url, ] ]; // 记录错误日志 return null; } } ```php
POST request:
public function httpPost(string $url = '', array $data = []) { // 记录请求信息的日志 // todo try { $jsonData = json_encode($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($jsonData) ]); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //https 请求 if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec($curl); $errorCode = curl_errno($curl); curl_close($curl); if (!empty($errorCode)) { // 可记录错误码日志 return null; } // 记录返回结果日志 return json_decode($result, true); } catch (\Exception $e) { $errorData = [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'data' => [ 'url' => $url, 'postData' => $data ] ]; // 记录错误日志 return null; } }
This article is part of Reprinted, the original address is:
https://tsmliyun.github.io/php/Basic usage of PHP’s CURL function extension/
Recommended: PHP Video Tutorial
The above is the detailed content of Basic usage of curl function extension in PHP. For more information, please follow other related articles on the PHP Chinese website!