0

我正在尝试使用 PHP 运行时在 appengine 中使用 gcm。以下是使用 URLFetch 服务的代码

$context = array("https"=>
             array( "method" => "post",
                "content" => json_encode($fields),
                "header" => "Content-Type: application/json\r\n" .
                            "Authorization: key=" . GOOGLE_API_KEY . "\r\n"
            )
        );
    $context = stream_context_create($context);
    $result = file_get_contents($url, false, $context);

以下是使用 PHP Curl 的原始代码:

    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);

使用 Curl 的 PHP 代码运行良好,但使用 appengine 的 urlfetch 服务的代码不起作用。有人可以告诉我我在哪里做错了。

4

1 回答 1

2

这是经过测试的代码。

    公共函数 sendAndroidPushNotification($registration_ids, $message)
    {
         // 在数组中添加设备令牌
         $registrationIds = 数组($registration_ids);
         $msg = 数组(
            '消息' => $消息,
            'title' => '通知中心',
            'tickerText' => $消息,
            '振动' => 1,
            '声音' => 1,
        );

        $字段 = 数组(
            'registration_ids' => $registrationIds,
            '数据' => $msg
        );
        $fields = json_encode($fields);
        $arrContextOptions=数组(
            “http” => 数组(
                “方法”=>“发布”,
                “标题” =>
                    '授权:密钥 = '。"\r\n" 。
                    “内容类型:应用程序/json”。"\r\n",
                “内容”=> $字段,
            ),
            “ssl”=>数组(
                “allow_self_signed”=>真,
                “verify_peer”=>假,
            ),
        );
        $arrContextOptions = stream_context_create($arrContextOptions);
        $result = file_get_contents('https://android.googleapis.com/gcm/send', false, $arrContextOptions);

        返回$结果;
    }
于 2014-12-20T10:32:33.597 回答