【PHP】协助前端解决跨域post请求

前言:虽然前端有蛮多解决跨域的方法,但都不是很好解决跨域的问题。但通过PHP作为媒介去解决跨域问题,是个很有效的方法。

一、方案一(速率较慢)

//index.php

$id = $_POST['id'];
$name = $_POST['name'];

function send_post($url, $post_data) {

    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result;
}

//跨域需提交的数据
$post_data = array(
    'mode' => 'edit',	
    'id' => $id,
    'name' => $name
);

$res = send_post('/*这里输入跨域请求的链接*/', $post_data);

echo $res;

写好跨域请求的php文件后,用ajax请求数据的时候请求该index.php就行了

$.ajax({
    type: 'post', 
    url: 'index.php',
    data: { id:'1', name:'freddy' },
    dataType: 'json',
    success: function(res){
	console.log(res);
    }     
});

二、方案二(速率较快)

//index.php

$id = $_POST['id'];
$name = $_POST['name'];

function send_post($url, $post_data){ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
	curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
	curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data)); // Post提交的数据包
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 是否对认证证书来源的检查
     
	//curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    //curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
    //curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    //curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 读取上面所储存的Cookie信息
	
    $tmpInfo = curl_exec($curl);   //执行操作
    if (curl_errno($curl)) {
        echo 'Errno'.curl_error($curl);
    }
    curl_close($curl);   // 关键CURL会话
    return $tmpInfo;     // 返回数据
}

//跨域需提交的数据
$post_data = array(
    'mode' => 'edit',	
    'id' => $id,
    'name' => $name
);

$res = send_post('/*这里输入跨域请求的链接*/', $post_data);
echo $res;

写好跨域请求的php文件后,用ajax请求数据的时候请求该index.php就行了

$.ajax({
    type: 'post', 
    url: 'index.php',
    data: { id:'1', name:'freddy' },
    dataType: 'json',
    success: function(res){
	console.log(res);
    }     
});

猜你喜欢

转载自blog.csdn.net/w390058785/article/details/80162029