我想使用php脚本访问oauth服务器,远程服务器需要以下请求:
POST /oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 81
Authorization: Basic CHAINE_ENCODEE
grant_type=password&username=USERNAME&password=PASSWORD&scope=all
所以我写了下面的代码:
$params = array ('grant_type' => 'password',
'username' => 'USERNAME',
'password' => hash('sha256', 'PASSWORD'),
'scope' => 'all',
);
$query = http_build_query ($params);
$contextData = array (
'protocol_version'=> '1.1',
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 81\r\n".
"Authorization: Basic ".base64_encode('APPIDENTIFIER:SECRET'),
'content' => $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents (
'https://rest2.some-url.net/oauth/token', // page url
false,
$context);
return $result;
但是这给了我以下错误:
Warning: file_get_contents(https://rest2.some-url.net/oauth/token): failed to open stream:
HTTP request failed! HTTP/1.0 500 Internal Server Error
我的猜测是问题来自我按照API要求设置为1.1的protocol_version。但错误显然使用HTTP / 1.0
我已经尝试了一切可能的方式,我看到没有人说它不适合他们所以我想我错过了一些东西。 为什么HTTP没有设置为HTTP / 1.1?