加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

GCM PHP – 传递二维数组

发布时间:2020-12-13 22:24:22 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用Google Cloud Messaging(GCM)将二维数组推送到设备.但是,我发现GCM一次只能发送一个数组. 我正在寻找解决这个问题的解决方案.因为我认为一个接一个地推送信息是不切实际的. 以下是两种不同的方案 单维数组(成功推送到设备) Array( [pump_name]
我正在尝试使用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,"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,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"
              }
}

然后你必须把它们洒到客户端

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读