<?php $scheme = 'http'; $host = '127.0.0.1'; $path = '/oauth2/token.php'; $data = array('grant_type' => 'client_credentials'); $user_pass = 'testclient:testpass';
if( get_extension_funcs('curl') ){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $scheme . '://' . $host . $path); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_USERPWD, $user_pass); curl_exec($ch); curl_close($ch); }else{ $data_str = http_build_query($data); $user_pass_encode = base64_encode($user_pass); $fp = @fsockopen($host, 80, $errno, $errstr, 30);
$header = "POST $path HTTP/1.1\r\n"; $header .= "Host: 127.0.0.1\r\n"; $header .= "Accept: */*\r\n"; $header .= "Content-length: " . strlen($data_str) . "\r\n"; $header .= "Authorization: Basic $user_pass_encode\r\n"; $header .= "Content-type: application/x-www-form-urlencoded\r\n"; $header .= "Connection: Close\r\n\r\n"; $header .= $data_str . '\r\n\r\n';
if ($fp) { $line = ''; fwrite($fp, $header); while (!feof($fp)) { $line .= fgets($fp, 256); } fclose($fp);
echo $line; } else { echo $errno, " ", $errstr; } }