惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园_首页
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
G
Google Developers Blog
Last Week in AI
Last Week in AI

Hi, I Am I

[I Am I 年度简报] — 不知终日梦为鱼 初探 ESP32-CAM QQ 聊天记录 MHT 文件转 HTML [I Am I 年度简报] - 草木本无意,荣枯自有时。 Hexo 中实现 Live Photos 支持 写在当下 NKCTF 2024 1z_F0r3ns1c5 Writeup 春秋杯冬季赛 2023 Writeup [I Am I 年度简报] - 2023 某内网渗透内部赛 Writeup 强网拟态 2023 Writeup Github Actions 自动化部署 Hexo 浅析CobaltStrike流量解密 陇剑杯 2023 Writeup CTF线下赛AWDP总结 ISCC 2023 Writeup ISCC 2023 实战题 Writeup CISCN 2023 Writeup 福建闽盾杯网络空间安全大赛 2023 Writeup 天一永安杯宁波市网络安全大赛 2023 Writeup 贵阳大数据及网络安全精英对抗赛 2023 Writeup 红明谷杯 2023 Writeup Confetti 带来有仪式感的鼓励 记一次 JS 逆向密码加密 [I Am I 年度简报] – 2022 PHP 读取 Excel 文件内容并写入数据库 从0开始的 MoeCTF 开发之路 观安杯 2022 Writeup 利用微信服务号实现早安自动化 Cloudflare批量拉黑IP脚本
PHP使用 CURL 发送网络请求
2020-08-20 · via Hi, I Am I

最近在开发 短视频去水印 服务以及扩展 TenAPI 的接口数量时,我封装了一个 CURL 请求方法。这里记录并分享一下这个方法。

CURL 函数

以下是我封装的 getCurl 函数,它支持 GET 和 POST 请求,可以选择性地处理 cookies 和自定义 HTTP 头部。

function getCurl(string $url, array $data = [], array $headers = [], bool $includeCookies = false): string {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    
    if ($includeCookies) {
        curl_setopt($curl, CURLOPT_HEADER, true);
    }

    if (!empty($headers)) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }

    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $result = curl_exec($curl);

    if (curl_errno($curl)) {
        $error = curl_error($curl);
        curl_close($curl);
        return 'Error: ' . $error;
    }

    if ($includeCookies) {
        preg_match('/Set-Cookie:(.*);/iU', $result, $str);
        $result = $str[1] ?? '';
    }

    curl_close($curl);
    return $result;
}

请求示例

GET 请求

$url = "https://api.example.com/";
$result = getCurl($url);
echo $result;

POST 请求

$url = "https://api.example.com/";
$data = [
    'key1' => 'value1',
    'key2' => 'value2'
];
$result = getCurl($url, $data);
echo $result;

自定义 HTTP 头部

$url = "https://api.example.com/";
$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer YOUR_ACCESS_TOKEN'
];
$result = getCurl($url, [], $headers);
echo $result;

接收 Set-Cookies

$url = "https://api.example.com/";
$result = getCurl($url, [], [], true);
echo $result;