问题
I'm developing a web application (using GAE for PHP) that notifies android clients (via GCM - Google Cloud Messaging) when some content are available for download.
The following PHP script should do job:
$json = array(
'data' => array( ... ),
'registration_ids' => array( ... )
);
$data = json_encode( $json );
$context = array(
'http' => array(
'method' => 'post',
'header' => 'Authorization: key=MY_SECRET_KEY' . "\r\n" .
'Content-Type: application/json' . "\r\n",
'content' => $data
)
);
$context = @stream_context_create($context);
$result = @file_get_contents("https://android.googleapis.com/gcm/send", false, $context);
The above code runs correctly when the app is deployed, but do not when running on my local development environment.
On local development environment $result is null and the file_get_contents "echo" the following warning failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed.
回答1:
I finally figured out what was happening.
The HTTP/1.0 405 Method Not Allowed was related with the 'method' => 'post'.
Belive me, simply changing it to 'method' => 'POST' (note the uppercase!) did the trick.
来源:https://stackoverflow.com/questions/20567824/gae-php-and-gcm-failed-to-open-stream-http-request-failed-http-1-0-405-metho