PHP fsockopen 으로 Authorization 인증시 참고코드
아래는 카카오 로그인 API 시에 Authorization 을 사용하는 예시이다.
// Initialize
$url = "https://kapi.kakao.com/v2/user/me";
$method = "GET";
$info = parse_url($url);
$req = '';
$data = '';
$line = '';
$agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';
$linebreak = "\r\n";
$headPassed = false;
// Setting Protocol
//echo $info['scheme'];exit;
switch($info['scheme'] = strtoupper($info['scheme']))
{
case 'HTTP':
$info['port'] = 80;
break;
case 'HTTPS':
$info['ssl'] = 'ssl://';
$info['port'] = 443;
break;
default:
return false;
}
// Setting Path
if(!$info['path'])
{
$info['path'] = '/';
}
// Setting Request Header
switch($method = strtoupper($method))
{
case 'GET':
if($info['query'])
{
$info['path'] .= '?' . $info['query'];
}
$req .= 'GET ' . $info['path'] . ' HTTP/1.1' . $linebreak;
$req .= 'Host: ' . $info['host'] . $linebreak;
$req .= 'User-Agent: ' . $agent . $linebreak;
$req .= 'Referer: ' . $url . $linebreak;
$req .= 'Authorization: Bearer '.$post['access_token'] . $linebreak;
$req .= 'Connection: Close' . $linebreak . $linebreak;
break;
case 'POST':
$req .= 'POST ' . $info['path'] . ' HTTP/1.1' . $linebreak;
$req .= 'Host: ' . $info['host'] . $linebreak;
$req .= 'User-Agent: ' . $agent . $linebreak;
$req .= 'Referer: ' . $url . $linebreak;
$req .= 'Authorization: Bearer '.$post['access_token'] . $linebreak;
$req .= 'Content-Type: application/x-www-form-urlencoded'.$linebreak;
$req .= 'Content-Length: '. strlen($info['query']) . $linebreak;
$req .= 'Connection: Close' . $linebreak . $linebreak;
$req .= $info['query'];
break;
}
//echo "req = {$req}\n";
// Socket Open
$fsock = @fsockopen($info['ssl'] . $info['host'], $info['port']);
if ($fsock)
{
fwrite($fsock, $req);
while(!feof($fsock))
{
$line = fgets($fsock, 128);
if($line == "\r\n" && !$headPassed)
{
$headPassed = true;
continue;
}
if($headPassed)
{
$data .= $line;
}
}
fclose($fsock);
}
$data = json_decode($data, true);