簡體   English   中英

GCM PHP - 傳遞二維數組

[英]GCM PHP - Passing Two-Dimensional Array

我正在嘗試使用Google Cloud Messaging(GCM)將二維數組推送到設備。 但是,我發現GCM一次只能發送一個數組。

我正在尋找解決這個問題的解決方案。 因為我認為一個接一個地推送信息是不切實際的。

以下是兩種不同的方案

單維數組(成功推送到設備)

Array
(
    [pump_name] => LEVO 92
    [pump_price] => 2.5
)
1
{"multicast_id":8959934119853137719,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576757470%9dde29c5f9fd7ecd"}]}
Array
(
    [pump_name] => LEVO 95
    [pump_price] => 3
)
1
{"multicast_id":6988128903201803494,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576797158%9dde29c5f9fd7ecd"}]}

二維數組(無法推送到設備)

Array
(
    [0] => Array
        (
            [pump_name] => LEVO 92
            [pump_price] => 2.5
        )

    [1] => Array
        (
            [pump_name] => LEVO 95
            [pump_price] => 3
        )

)
1
Field "data" must be a JSON array: [{"pump_name":"LEVO 92","pump_price":"2.5"},{"pump_name":"LEVO 95","pump_price":"3"}]

代碼(我把它分成兩部分)

泵價格功能

function pump_price() {

    global $wpdb;

    $result = array();
    $temp_result = array();
    $counter = 1;

    $rows = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_value FROM postmeta WHERE meta_key LIKE %s", 'pump_price_list_%', 'pump_%'));

    if($rows) :
        foreach($rows AS $row) :
            if($counter % 2 == 0) :

                // Store Value Into Temporary Array
                $temp_result["pump_price"] = $row->meta_value;

                array_push($result, $temp_result);

                // Unset Temporary Array
                unset($temp_result);
                $temp_result = array();

            else :

                $temp_result['pump_name'] = $row->meta_value;

            endif;
            $counter++;
        endforeach;
    endif;

    sendGoogleCloudMessage($result);

}

GCM功能

function sendGoogleCloudMessage($result) {

    define( 'API_ACCESS_KEY', 'MY-API-KEY' );

    $registrationIds = array('MY-DEVICE-ID');

    $headers = array (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

    $fields = array (
        'registration_ids'  => $registrationIds,
        'data'              => $result
    );


    $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 ) );
    $results = curl_exec($ch );
    curl_close( $ch );

    echo $results;
}

您正在發送2個JSON,這就是問題所在。 您可以將它們組合成如下所示:

{  
    "array1": {
               "pump_name":"LEVO 92",
               "pump_price":"2.5"
              },
    "array2": {
               "pump_name":"LEVO 95",
               "pump_price":"3"
              }
}

然后你必須把它們灑到客戶端

嗨,為那些尋找解決此問題的人。

在提到@Ali建議后,我設法找到了解決方案。

但是,在傳遞二維數據或更多數據之前,需要更改一些代碼。 請閱讀以下解決方案。

將數組轉換為Object

if($rows) :
    foreach($rows AS $row) :
        if($counter % 2 == 0) :

            // Store Value Into Temporary Array
            $temp_result["pump_price"] = $row->meta_value;

            array_push($result, (object)$temp_result);

            // Unset Temporary Array
            unset($temp_result);
            $temp_result = array();

        else :

            $temp_result['pump_name'] = $row->meta_value;

        endif;
        $counter++;
    endforeach;
endif;

sendGoogleCloudMessage((object)$result);

如果這不是一個理想的解決方案,請糾正我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM