一、CURL函数简介
    PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器、使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传(也能通过 PHP 的 FTP 扩展完成)、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。
    二、官网上的引例
    只要你编译完的PHP设置了支持 cURL 扩展,你就可以开始使用cURL函数了。使用 cURL 函数的基本思想是先使用 curl_init() 初始化 cURL会话,接着可以通过 curl_setopt() 设置需要的全部选项,然后使用 curl_exec() 来执行会话,当执行完会话后使用 curl_close() 关闭会话。这是一个使用 cURL 函数获取 example.com 主页保存到文件的例子:
    引例1:通过CURL获取exampl.com的主页

    1. <?php
    2. $ch = curl_init("http://www.example.com/");
    3. $fp = fopen("example_homepage.txt", "w");
    4. curl_setopt($ch, CURLOPT_FILE, $fp);
    5. curl_setopt($ch, CURLOPT_HEADER, 0);
    6. curl_exec($ch);
    7. curl_close($ch);
    8. fclose($fp);
    9. ?>

    引例2:通过CURL带参数获取myremoteservice主页

    <?php
    <?php
    $params=['name'=>'John', 'surname'=>'Doe', 'age'=>36)
    $defaults = array(
    CURLOPT_URL => 'http://myremoteservice/',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $params,
    );
    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    ?>
    ?>
    

    三、独立学习Demo
    1、目录结构:
    —-demo
    +———-get.php
    +———-index.php

    index.php

    <?php
    header("Content-Type:text/html; charset=utf-8");
    $course = isset($_REQUEST['course']) ? $_REQUEST['course'] : 0;
    $depid = isset($_REQUEST['depid']) ? $_REQUEST['depid'] : 0;
    $data = "course=$course&depid=$depid";
    $curlobj = curl_init();
    // 设置获取数据url
    curl_setopt($curlobj, CURLOPT_URL, "http://localhost/api/curl-2/get/get.php");
    // 设置传递信息报文格式
    curl_setopt($curlobj, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0");
    // 设置不使用数据流输出
    curl_setopt($curlobj, CURLOPT_HEADER, 0);
    // 如果成功返回,不成功返回false,不自动输出任何内容
    curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
    // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
    curl_setopt($curlobj, CURLOPT_POST, 1);
    // 发送参数
    curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
    // 发送参数,报文格式 
    curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8","Content-length: ".strlen($data)));
    // 执行curl函数抓取
    $rtn = curl_exec($curlobj);
    print_r($rtn);
    ?>
    

    get.php

    <?php
    header("Content-Type:text/html; charset=utf-8");
    $host = 'localhost';       // 连接数据库地址或IP
    $root = 'root';           // 连接账号 
    $pwd = '';               // 连接密码
    $db = 'class';          // 数据库名(可自定义)
    
    $link = @new mysqli($host, $root, $pwd, $db);
    $link->set_charset("utf8");
    
    if(!$link->connect_errno) {
        // 查询条件1
        if(isset($_REQUEST['course'])) {
            // 判断接收参数 0 => null
            if($_REQUEST['course']) {
                $sql = "select * from course where id =".$_REQUEST['course'];
                $result = $link->query($sql);
                // var_dump($result);
                if($result->num_rows) {
                    while($row = $result->fetch_assoc()) {
                        $data['course'][]= $row;
                    }
                }
    
            }
        }
            // 查询条件2
        if(isset($_REQUEST['depid'])) {
            // 判断接收参数 0 => null 
            if ($_REQUEST['depid']) {
                $sql = "select * from dep where id =".$_REQUEST['depid'];
                // var_dump($sql);exit;
                $result = $link->query($sql);
                // var_dump($result);
                if($result->num_rows) {
                    while($row = $result->fetch_assoc()) {
                        $data['dep'][]= $row;
                    }
                }
    
            } 
        }
            // 结果输出
        if($data) {
            echo json_encode($data);
        }
    
    } else {
        // 连接数据库失败
        die('This is a mistake!');
    }
    
    ?>
    

    【参考资料】不是一般的哦,要好好看哦^~^
    https://www.zhihu.com/question/28207685
    http://taobaofed.org/
    http://2014.jsconf.cn/slides/herman-taobaoweb
    http://blog.jobbole.com/65509/
    https://blog.kaolafed.com/