https的curl post

这段PHP代码实现了一个通过CAS(Central Authentication Service)进行OAuth2.0授权的流程,首先检查请求中的`code`参数,若为空则重定向至授权页面。接着使用`code`换取访问令牌,并利用该令牌获取用户信息。最后,将用户信息中的ID作为参数,重定向至特定登录检查页面。
摘要由CSDN通过智能技术生成
<?php
set_time_limit(0); 
 header("Content-Type: text/html;charset=utf-8");
    //告诉浏览器不要缓存数据
    header("Cache-Control: no-cache");  
$code=$_GET["code"];
//var_dump($code);
//echo $code."111";
//exit;
if($code==NULL){
	$url5="https://cas.hrbcu.edu.cn/cas/oauth2.0/authorize?response_type=code&client_id=jeTVmqw8a2iF96OiPN&redirect_uri=https://xt.hrbcu.edu.cn/rz.php";
	
	header("Location:".$url5);
	}
//echo $code."<br>";
//exit;
//echo file_get_contents("http://www.baidu.com");
$url="https://cas.h.cn/cas/oauth2.0/accessToken?client_id=jeTVmqw8a2iF96OiPN&client_secret=1b365b69ec254707b4ee84ef0000c0e4&code=".$code."&redirect_uri=https://xt.hrbcu.edu.cn/rz.php";  

//echo $url."<br>";

//header("Location:".$url);
//exit;
//$html = fopen($url,"r");
//$aaaa=file_get_contents("http://5.4/cas/oauth2.0/accessToken?client_id=jeTVmqw8a2iF96OiPN&client_secret=1b365b69ec254707b4ee84ef0000c0e4&code=".$code."&redirect_uri=http://12.96/ceshi.php"); 
$param="";
$postUrl = $url;
$curlPost = $param;

 $ch = curl_init(); 
 /*$timeout = 5;  
 curl_setopt ($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
 $file_contents = curl_exec($ch); 
 curl_close($ch); */
 
 $timeout = 5; 
 curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);//运行curl
        curl_close($ch);
 
 
//echo $file_contents."555";
$result = json_decode($file_contents, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

$access_token = $result["access_token"];
//echo  $access_token;


$url1="https://cas.hrbcu.edu.cn/cas/oauth2.0/profile?access_token=".$access_token."";
//echo $url1;
$param1="";
$postUrl1 = $url1;
$curlPost1 = $param1;

$ch1 = curl_init(); 

 curl_setopt($ch1, CURLOPT_URL,$postUrl1);//抓取指定网页
        curl_setopt($ch1, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch1, CURLOPT_POST, 1);//post提交方式
		curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
		curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $curlPost1);
        $file_contents1 = curl_exec($ch1);//运行curl
		
		
        curl_close($ch1);

 /*$timeout1 = 5;  
 curl_setopt ($ch1, CURLOPT_URL, $url1); 
 curl_setopt($ch1, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
 curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);  
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_setopt ($ch1, CURLOPT_CONNECTTIMEOUT, $timeout1); 
 $file_contents1 = curl_exec($ch1); 
 curl_close($ch1); */
$result1 = json_decode($file_contents1, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

//var_dump($result1);
$username=$result1["id"];
//echo $id;

//$username=urlencode($id);
//echo $username;
$url2="http://xt.hrbcu.edu.cn/logincheck_new.php?username=".$username;
//echo "<br>";
//echo $url2;
?>
<script>location.href="<?=$url2?>";</script>
<?
//header("Location:".$url2);

exit;

 
/*$ch = curl_init();

$timeout = 10; 

curl_setopt ($ch, CURLOPT_URL,'$url');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$url = curl_exec($ch);*/


echo $url."<br>";
$result = json_decode($url, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

$access_token = $result["access_token"];
echo $access_token;
exit;







/*$url="http://.3:1003/open/accessToken?client_id=vTG041gw&response_type=code&scope=baseInfo&client_secret=BcXxkw&grant_type=authorization_code&code=$access_code&redirect_uri=http://60.219.114.4:8081/ceshi.php";  
//echo $url;
$html = file_get_contents($url);  
echo $html;  

$result = json_decode($html, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result["data"]["access_token"];
echo $access_token;


$url1="http://6.219.14.3:1003/open/userInfo?access_token=$access_token";

$html1 = file_get_contents($url1);

echo $html1;

$result1 = json_decode($html1, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$username = $result1["data"]["user"]["username"];
echo $username;


//file_get_contents($url2);*/



?>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hai7425

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值