Home >Backend Development >PHP Tutorial > 请教QQ互联的代码是如何写的?

请教QQ互联的代码是如何写的?

WBOY
WBOYOriginal
2016-06-13 12:34:41867browse

请问QQ互联的代码是怎么写的??

<?php <br />
  //应用的APPID<br />
  $app_id = "xxxxx";<br />
  //应用的APPKEY<br />
  $app_secret = "xxxxxxx";<br />
  //成功授权后的回调地址<br />
  $my_url = "http://www.abc.com";<br />
 <br />
  //Step1:获取Authorization Code<br />
  session_start();<br />
  $code = $_REQUEST["code"];<br />
  if(empty($code)) <br />
  {<br />
     //state参数用于防止CSRF攻击,成功授权后回调时会原样带回<br />
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); <br />
     //拼接URL     <br />
     $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" <br />
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="<br />
        . $_SESSION['state'];<br />
     echo("<script> top.location.href='" . $dialog_url . "'</script>");<br />
  }<br />
 <br />
  //Step2:通过Authorization Code获取Access Token<br />
  if($_REQUEST['state'] == $_SESSION['state']) <br />
  {<br />
     //拼接URL   <br />
     $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"<br />
     . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)<br />
     . "&client_secret=" . $app_secret . "&code=" . $code;<br />
     $response = file_get_contents($token_url);<br />
     if (strpos($response, "callback") !== false)<br />
     {<br />
        $lpos = strpos($response, "(");<br />
        $rpos = strrpos($response, ")");<br />
        $response  = substr($response, $lpos + 1, $rpos - $lpos -1);<br />
        $msg = json_decode($response);<br />
        if (isset($msg->error))<br />
        {<br />
           echo "<h3>error:</h3>" . $msg->error;<br />
           echo "<h3>msg  :</h3>" . $msg->error_description;<br />
           exit;<br />
        }<br />
     }<br />
 <br />
     //Step3:使用Access Token来获取用户的OpenID<br />
     $params = array();<br />
     parse_str($response, $params);<br />
     $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];<br />
     $str  = file_get_contents($graph_url);<br />
     if (strpos($str, "callback") !== false)<br />
     {<br />
        $lpos = strpos($str, "(");<br />
        $rpos = strrpos($str, ")");<br />
        $str  = substr($str, $lpos + 1, $rpos - $lpos -1);<br />
     }<br />
     $user = json_decode($str);<br />
     if (isset($user->error))<br />
     {<br />
        echo "<h3>error:</h3>" . $user->error;<br />
        echo "<h3>msg  :</h3>" . $user->error_description;<br />
        exit;<br />
     }<br />
     echo("Hello " . $user->openid);<br />
  }<br />
  else <br />
  {<br />
     echo("The state does not match. You may be a victim of CSRF.");<br />
  }<br />
?>

这是在官网弄下来的代码,
首先在网站上点击超链接,然后跳转到这个php文件,接下来怎么做呢,
云里雾里,一头雾水

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn