PHP实现跨域解决方法
如果要实现跨域通过设置Access-Control-Allow-Origin来实现跨域。
例如:客户端的域名是client.runoob.com,而请求的域名是server.runoob.com。
如果直接使用ajax访问,会有以下错误:
XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.
1、允许单个域名访问
指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:http://client.runoob.com');
2、允许多个域名访问
指定多个域名(http://client1.runoob.com、http://client2.runoob.com等)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
'http://client1.runoob.com',
'http://client2.runoob.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}
3、允许所有域名访问
允许所有域名访问则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:*');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>跨域1</title>
</head>
<body>
</body>
</html>
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$.ajax({
type: 'post',
url: 'corssOrigin1.php',
data: { id:'1', name:'limenglong' },
dataType: 'json',
success: function(res){
console.log(res);
}
});
</script>
php代码
方案1:
$id = $_POST["id"];
$name = $_POST['name'];
function send_post($url, $post_data) {
//http_build_query()构造URL字符串的方法(可将POST参数组转换拼接成GET请求链接)
$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); //模拟post 或者get请求
$result = file_get_contents($url, true, $context);//把文件写入一个字符串
return $result;
}
//跨域需提交的数据
$post_data = array(
'mode' => 'edit',
'id' => $id,
'name' => $name
);
$res = send_post('https://www.baidu.com/cache/sethelp/help.html', $post_data);
echo $res;
方案2:
<?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;
2877

被折叠的 条评论
为什么被折叠?



