问题
I am getting following error while posting request from my server to google.com:
Warning: file_get_contents(http://accounts.google.com): failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed in C:\...\index.php on line 23
My code is following:
$postdata = http_build_query(
array(
'code' => '####',
'client_id' => '####',
'client_secret' => '####',
'grant_type' => 'authorization_code'
)
);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://accounts.google.com', false, $context);
echo $result;
回答1:
the method you have chosen is 'PUT' and not 'POST'. if you want to send request as POST then change
'method' => 'PUT'
to
'method' => 'POST'
回答2:
First of all what you get here is a response code (405) and it is within the error class (400 to 499 or also written as 4xx).
So the server reports back to you an error on the protocol level. The protocol used is named as well: HTTP, more specifically HTTP/1.0.
HTTP/1.0 including the status codes are outlined in RFC1945:
- http://tools.ietf.org/html/rfc1945
The problem here is that the code 405 (as the concrete number) is not defined. So the docs fall back to the general description of 4xx for error codes.
HTTP/1.1 is outlined in RFC2616:
- http://tools.ietf.org/html/rfc2616
It has the 405 error code details however, as the response headers in your code example show:
// ...
$result = file_get_contents('http://accounts.google.com', false, $context);
var_dump($http_response_header);
Output:
array(6) {
[0] => string(31) "HTTP/1.0 405 Method Not Allowed"
[1] => string(38) "Content-Type: text/html; charset=UTF-8"
[2] => string(19) "Content-Length: 958"
[3] => string(35) "Date: Thu, 24 Oct 2013 12:03:09 GMT"
[4] => string(15) "Server: GFE/2.0"
[5] => string(27) "Alternate-Protocol: 80:quic"
}
The required response listing shows another protocol violation as the required Allow header is not part of the response. It would have shown which methods are allowed.
So all you can do is to try if common HTTP methods work instead of PUT, you might be looking for POST. Let's run the example with POST, but no luck:
Warning: file_get_contents(http://accounts.google.com): failed to open stream: HTTP request failed! HTTP/1.0 405 HTTP method POST is not supported by this URL
I suggest you contact Google (the vendor of that online service you send HTTP requests to) and ask about the technical specification you need to match for the request you had in mind. For debugging purposes I suggest you read about PHP's special $http_response_header variable, I have some example code and explanation as well as some hints on error handling with PHP's HTTP Wrapper in:
- HEAD first with PHP Streams (Sep 2011; by hakre)
回答3:
Using the wrong URI, (which is why you get no allowed methods header). Access tokens are retrieved via https://accounts.google.com/o/oauth2/token "https" is required.
Unrelated: Your postdata is missing the required "redirect_uri".
回答4:
This is quite late, hope it helps someone else like me.
There are two issues. First of all an access token cannot be generated with client_secret
. So to have a successful request client_secret
must be included.
Secondly google wants to identify the client_id
, so it should also be a part of POST
parameters.
The correct parameters for generating access tokens should be like
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata,
'client_id' => '######################',
'client_secret' => '##########################'
)
)
来源:https://stackoverflow.com/questions/19565191/post-request-not-supported-by-google-server