【问题标题】:How can I send this GCM downstream message (HTTP POST REQUEST) using php?如何使用 php 发送此 GCM 下游消息(HTTP POST REQUEST)?
【发布时间】:2015-12-12 23:55:30
【问题描述】:

我对 php 语法相当陌生,我了解如何接收 http post 请求,以及 php 中的 GET 请求,但我不知道如何在没有表单的情况下在 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"
}

【问题讨论】:

  • GCM 的 php downstream message 在这里检查:androidbegin.com/tutorial/… 在下面搜索 Create a simple PHP server to communicate with GCM.,它包含 gcm_engine.php 这是用于此的 php 脚本

标签: php android ajax google-cloud-messaging http-post


【解决方案1】:

此代码适用于 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;
?>

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 2016-01-07
    相关资源
    最近更新 更多