我对php语法相当陌生,我知道如何接收http post请求,也知道如何在php中获取请求,但我不知道如何使用下面的数据在没有表单的情况下正确地在php中发送http post请求。(我知道我可以使用ajax,但我想学习如何使用php脚本)。
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=I_PUT_THIS_AS_A_SAMPLE
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to": "SAMPLE_SAMPLE"
}
此代码适用于Android。
试着以底部为例。您可以将此代码粘贴到http://phpfiddle.org/.尝试理解逻辑流是如何工作的,并通过添加您想要发送的消息(即分数和时间)来处理代码。我在实现推送通知时发现了这段代码。为了使下面的代码正常工作,您需要一个授权密钥以及设备注册令牌。祝你好运
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Axxxxxxxxxxxxxxxxxx' );
$registrationIds = array("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>