duanjianhe1388 2014-09-23 02:48 采纳率: 100%
浏览 34

使用curl_multi_exec和gcm推送通知和PHP

I currently have an app configured to send push notification via Google/GCM. This is working fine, however there is a 1000 limit per push notification. Currently, I have to loop the registration ID's into separate messages and send them in batches. My question is, I am not very familiar with CURL. How can I utilize curl_mutli_exec to speed up the push notifications and get messages to users with less of a delay.

Here is the current way I have it set up. What do I need to do to utilize curl_mutli_exec? Thanks!

$file = fopen("/gcmusers.key", "r") or exit("Unable to open file!");
        //Output a line of the file until the end is reached
        $pcount = 0;
        $registrationIDs = array();
        while(!feof($file)) {

                $pid = fgets($file);

                $pcount = $pcount + 1;
                if ($pid <> '') {
                        array_push($registrationIDs,$pid);
                }

                if ($pcount == 990) {
                        // Message to be sent

                        $apiKey = "MyAPIKEY";

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

                        $fields = array('registration_ids'  => $registrationIDs, 'data' => array( "message" => $message, "pic" => $pic, "price" => $price, "item" => $item, "site" => $site, "refurb" => $refurb, "percent" => $percent, "buyurl" => $buyurl ),);

                        $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 ) );

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

                        // Close connection
                        curl_close($ch);

                        $registrationIDs = array();
                        $pcount = 0;

                }

        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏