1

私は CGM サーバーに通知を送信するための php スクリプトに取り組んでおり、次の例から取り組んでいます。

public function send_notification($registatoin_ids, $message) { // 設定を含める include_once './config.php';

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $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);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo $result;
}

しかし、変数の値がどうあるべきかはわかりません: CURLOPT_POSTFIELDS 、 CURLOPT_SSL_VERIFYPEER 、 CURLOPT_RETURNTRANSFER 、 CURLOPT_HOST 、 CURLOPT_URL

これらの値がどうあるべきかを知っている人はいますか?

ありがとうございました!

4

1 に答える 1

2

" CURLOPT_POSTFIELDS " このフィールドは、json エンコーディング形式のフィールド数をプッシュするために必要です。"CURLOPT_SSL_VERIFYPEER" このフィールドは、サード パーティ サーバーが SSL 経由の HTTPS である場合に必要です。"CURLOPT_RETURNTRANSFER" このフィールドは、gcm サーバーからの応答を返します。"CURLOPT_HOST , CURLOPT_URL" このフィールドは、サード パーティ サーバーのホスト名と URL を定義する必要があります。

それは私が実装したコードです:

<?php
$apiKey = "Your Api Key"; 
$reg_id = array("Your registration ID that we have get from device");
$registrationIDs = $reg_id;

// Message to be sent
$message = $_REQUEST['message']; 

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
          'registration_ids' => $registrationIDs,
          'data' => array( "message" => $message ),
           );
$headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
         );

// Open connection
$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 );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
?>

私の経験によると、このリンクはあなたを助けます:

PHP を使用した GCM (Google クラウド メッセージング)

http://www.sherif.mobi/2012/07/gcm-php-push-server.html

https://groups.google.com/forum/#!topic/android-gcm/hjk5PUYlTp0

于 2012-10-20T04:08:17.547 に答える