PHP调用接口:POST&GET

环境部署:Ubuntu部署PHP

1 Post请求

<?php
/**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        // 传输数据转为json格式
        $json_data = json_encode($post_data);
        $postUrl = $url;
        $curlPost = $json_data;
        //初始化curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        // post提交方式
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        // Header配置
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset=utf-8',
        'Content-Length:'.strlen($curlPost)));
        // 运行curl
        $data = curl_exec($ch);
        curl_close($ch);
        
        return $data;
        // json数据
        // return json_decode($data, true);
    }

    function testAction(){
        $url = "http://localhost:8090/api/facebase/user/search";
        $post_data["u_id"] = "001";
        $post_data["u_name"] = "小二";
        $post_data["u_phone"] = "1245";
        $res = request_post($url, $post_data); 
        // echo $res;    
        print_r($res);

    }
    // 执行调用函数,并输出结果
    echo testAction();
?>php

2 Get请求

<?php
/**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        echo "json data:\n";
        $json_data = json_encode($post_data);
        echo $json_data;
        echo "----\n";
        $o = "";
        // 遍历数组,拼接为字符串,如:admin_name=admin&admin_phone=1245
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        
        $post_data = substr($o,0,-1);
        // 拼接请求地址:http://localhost:8090/api/facebase/admin?admin_name=admin&admin_phone=1245
        $url_demo = $url."?".$post_data;
        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url_demo);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        // 设置Header
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset=utf-8',
        'Content-Length:'.strlen($curlPost)));
        //运行curl
        $data = curl_exec($ch);
        curl_close($ch);
        // 返回原始结果数据
        return $data;
        // json数据[]:[]
        // return json_decode($data, true);
    }

    function testAction(){
        $url = "http://localhost:8090/api/facebase/admin";
        // $post_data["u_id"] = "001";
        $post_data["admin_name"] = "admin";
        $post_data["admin_phone"] = "1245";
        $res = request_post($url, $post_data);
        print_r($res);

    }
    // 执行调用函数,并输出结果
    echo testAction();
?>php

3 小结

(1) Get请求,需要将传入参数存入数组中,并遍历数据将数据作拼接,最后还需要对URL进一步拼接,形成get的请求格式;
(2) Post请求,需要向传入的参数,转化为json数据;
(3) 设置httpheader,根据需要配置相应参数.
(4) 使用curl方式进行请求,需要安装php-curl,sudo apt-get install php-curl.


【参考文献】
[1]https://blog.csdn.net/pangchengyong0724/article/details/52103962
[2]https://www.php.cn/php-weizijiaocheng-372263.html
[3]https://blog.csdn.net/qq_36775515/article/details/53504436
[4]https://blog.csdn.net/Xin_101/article/details/98378887

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值