Basic usage of curl function extension in PHP

藏色散人
Release: 2023-04-08 13:54:02
forward
2061 people have browsed it

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); // 释放资源
Copy after login

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);
Copy after login

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
Copy after login

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; } }
Copy after login

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!

Related labels:
php
source:github.io
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!