GET 和POST 的curl封装函数

GET请求:

function get($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $SSL = substr($url, 0, 8) == "https://" ? true : false;
    if ($SSL) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名
    }
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

POST请求:

function post($url, $params = [])
{
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:1314/api/' . $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);

    //$token = session('token');

    //curl_setopt($curl, CURLOPT_HTTPHEADER, ["XX-Token: $token"]);
    //设置post数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据

    return json_decode($data, true);
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是示例代码,对应四个接口,使用了CURL库进行封装,其中有注释说明参数的含义: ```c #include <stdio.h> #include <curl/curl.h> // HTTPS GET请求 int https_get(char *url, char *response) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { // 设置请求的URL curl_easy_setopt(curl, CURLOPT_URL, url); // 设置SSL验证方式 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // 设置返回数据的回调函数和回调函数参数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); // 执行请求 res = curl_easy_perform(curl); // 检查请求是否成功 if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return -1; } curl_easy_cleanup(curl); } return 0; } // HTTPS POST请求 int https_post(char *url, char *postdata, char *response) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { // 设置请求的URL curl_easy_setopt(curl, CURLOPT_URL, url); // 设置SSL验证方式 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // 设置POST请求的参数 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); // 设置返回数据的回调函数和回调函数参数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); // 执行请求 res = curl_easy_perform(curl); // 检查请求是否成功 if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return -1; } curl_easy_cleanup(curl); } return 0; } // HTTP GET请求 int http_get(char *url, char *response) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { // 设置请求的URL curl_easy_setopt(curl, CURLOPT_URL, url); // 设置返回数据的回调函数和回调函数参数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); // 执行请求 res = curl_easy_perform(curl); // 检查请求是否成功 if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return -1; } curl_easy_cleanup(curl); } return 0; } // HTTP POST请求 int http_post(char *url, char *postdata, char *response) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { // 设置请求的URL curl_easy_setopt(curl, CURLOPT_URL, url); // 设置POST请求的参数 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); // 设置返回数据的回调函数和回调函数参数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); // 执行请求 res = curl_easy_perform(curl); // 检查请求是否成功 if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return -1; } curl_easy_cleanup(curl); } return 0; } // 返回数据的回调函数 size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { strcat(userdata, ptr); return size * nmemb; } ``` 以上代码使用了回调函数write_callback来处理返回的数据,可以根据自己的需求进行修改。同时,需要注意curl库的安装和链接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值