搜索
博主信息
博文 142
粉丝 5
评论 0
访问量 157076
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
数据处理文件post.php
php开发大牛
原创
996人浏览过

接收post.html页面传过来的数据,并发送请求然后处理请求结果,前端页面传过来的都是Body请求参数,如果还需要Header参数的话,可以在这个文件手动添加上去。


<?php

echo '<title>API接口请求响应</title>';

/**

 * 设置网络请求配置

 * @param [string] $curl 请求的URL

 * @param [bool] true || false 是否https请求

 * @param [string] $method 请求方式,默认GET

 * @param [array] $header 请求的header参数

 * @param [object] $data PUT请求的时候发送的数据对象

 * @return [object] 返回请求响应

 */

function ihttp_request($curl,$https=true,$method='GET',$header=array(),$data=null){

    // 创建一个新cURL资源

    $ch = curl_init();

     

    // 设置URL和相应的选项

    curl_setopt($ch, CURLOPT_URL, $curl); //要访问的网站

    //curl_setopt($ch, CURLOPT_HEADER, false);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if($https){

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);

    }

    if($method == 'POST'){

        curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    }

     

     

    // 抓取URL并把它传递给浏览器

    $content = curl_exec($ch);

    if ($content === false) {

     return "网络请求出错: " . curl_error($ch);

     exit();

    }

    //关闭cURL资源,并且释放系统资源

    curl_close($ch);

     

    return $content;

}

//检查是否是链接格式

function checkUrl($C_url){

 $str="/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/";

 if (!preg_match($str,$C_url)){

 return false;

 }else{

        return true;

 }

}

//检查是不是HTTPS

function check_https($url){

    $str="/^https:/";

    if (!preg_match($str,$url)){

 return false;

 }else{

        return true;

 }

}

if($_SERVER['REQUEST_METHOD'] != 'POST') exit('请求方式错误!');

//发送请求

function curl_query(){

    $data = array(

        $_POST['key1'] => $_POST['value1'],

        $_POST['key2'] => $_POST['value2'],

        $_POST['key3'] => $_POST['value3'],

        $_POST['key4'] => $_POST['value4'],

        $_POST['key5'] => $_POST['value5'],

        $_POST['key6'] => $_POST['value6']

    );

    //数组去空

    $data = array_filter($data);                    //post请求的参数

    if(empty($data)) exit('请填写参数');

     

    $url = $_POST['curl'];                          //API接口

    if(!checkUrl($url)) exit('链接格式错误');     //检查连接的格式

    $is_https = check_https($url);              //是否是HTTPS请求

    $method = $_POST['method'];                     //请求方式(GET POST)

     

    $header = array();                              //携带header参数

    //$header[] = 'Cache-Control: max-age=0';

    //$header[] = 'Connection: keep-alive';

     

    if($method == 'POST'){

        $res = ihttp_request($url,$is_https,$method,$header,$data);

        print_r(json_decode($res,true));

    }else if($method == 'GET'){

        $curl = $url.'?'.http_build_query($data);   //GET请求参数拼接

        $res = ihttp_request($curl,$is_https,$method,$header);

        print_r(json_decode($res,true));

    }else{

        exit('error request method');

    }

}

curl_query();


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学