zoukankan      html  css  js  c++  java
  • PHP——封装Curl请求方法支持POST | DELETE | GET | PUT 等

    前言

    Curl:  https://www.php.net/manual/en/book.curl.php

    curl_setopt: https://www.php.net/manual/en/function.curl-setopt.php

    代码

    注释应该很详细了吧,不懂的可以评论或者看上面手册。

    设置桥接抓包需要配合抓包工具使用。

    /**
     * 多种请求方法封装
     * 
     * @param string   $url      请求地址
     * @param string   $method   请求方式
     * @param array    $header   请求头
     * @param array    $data     请求体
     * 
     * @return mixd 
     */
    function Curl_request($url, $method = 'POST', $header = ["Content-type:application/json;charset=utf-8", "Accept:application/json"], $data = [])
    {
        
        $method = strtoupper($method);
        //初始化
        $ch = curl_init();
        //设置桥接(抓包)
        //curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
        //设置请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        // 检查ssl证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 从检查本地证书检查是否ssl加密
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $url);
        //设置请求方法
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        //设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        //设置请求数据
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //设置curl_exec()的返回值以字符串返回
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        curl_close($ch); 
        return $res;
    }
  • 相关阅读:
    博客园的界面设置
    ARM 汇编指令集
    winfroms更换皮肤
    面向对象的七项设计原则
    S2-01
    机票查询与订购系统
    重点语法
    第二章
    一、17.09.13
    习作
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/10871321.html
Copyright © 2011-2022 走看看