Passport.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Cache\Memcached;
  4. use KIF\Core\Config;
  5. use KIF\Core\Request;
  6. use Cas\Dao\UserInfo;
  7. /**
  8. *
  9. * passport 从第三方获取userid
  10. * @author rabin
  11. *
  12. */
  13. class Passport extends Controller{
  14. private $config;
  15. public function __construct() {
  16. $this->config = Config::getInstance()->get('passport');
  17. //$this->objMemcached = new Memcached();
  18. //$this->refer = 'passportRefer';
  19. }
  20. public function doGet() {
  21. $refer = Request::g ( 'referer' );
  22. $cas_uid = Request::g ( 'cas_uid' );
  23. $tokenid = Request::g ( 'tokenid' );
  24. $objUserData = new UserInfo();
  25. $userData = $objUserData -> get($cas_uid);
  26. //$this->objMemcached->set($this->refer, $refer);
  27. $host = $this->config['url'];
  28. $param = $this->config['param'];
  29. /*
  30. if ($param) {
  31. foreach ($param as $k => $v) {
  32. if ($v == '{passport_uid}') {
  33. if (isset($userData['passport_uid']) && $userData['passport_uid']) {
  34. $param[$k] = $userData['passport_uid'];
  35. } else {
  36. $param[$k] = $v = $tokenid;
  37. }
  38. }
  39. if (!$v) {
  40. unset($param[$k]);
  41. }
  42. }
  43. }
  44. */
  45. $param['authorizedCode'] = 'fulishe';
  46. $param['tokenId'] = $tokenid;
  47. //echo $this->config['url'] . "\r\n";
  48. //print_r($param);
  49. $data = $this->httpPost($host, $param);
  50. $data = json_decode($data, true);
  51. /*
  52. $data['data'] = array
  53. (
  54. 'nickName' => 'rabin',
  55. 'avatar' => 'http://echarts.baidu.com/images/logo.png',
  56. 'tokenId' => '111111',
  57. );
  58. */
  59. if (isset($data['data']) && isset($data['data']['userId']) && $data['data']['userId']) {
  60. $refer = urldecode($refer);
  61. $refer = str_replace('&amp;', '&', $refer);
  62. $param['nickname'] = $data['data']['nickName'];
  63. $param['headimgurl'] = $data['data']['avatar'];
  64. $param['uid'] = $data['data']['userId'];
  65. $refer .= '&' . http_build_query($param);
  66. header ( "Location: " . $refer);
  67. } else {
  68. echo $tokenid . "<br />";
  69. print_r($data);die;
  70. }
  71. }
  72. private function httpGet($url) {
  73. $curl = curl_init();
  74. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  75. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  76. curl_setopt($curl, CURLOPT_URL, $url);
  77. $res = curl_exec($curl);
  78. curl_close($curl);
  79. return $res;
  80. }
  81. private function httpPost($url, $param) {
  82. $curl = curl_init();
  83. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  84. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  85. curl_setopt($curl, CURLOPT_URL, $url);
  86. curl_setopt($curl, CURLOPT_POST, 1);
  87. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  88. $res = curl_exec($curl);
  89. curl_close($curl);
  90. return $res;
  91. }
  92. }