drelgkxl93433 2016-09-11 10:05
浏览 39

如何在php中拆分冗长的URI

I am sending bulk SMS with php API through a SMS gatway like this:

http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=11111,22222,33333,44444,55555,66666.... 

(upto 1800 mobile numbers which is stored in mysqldb). But the service provider only allow me to send only 50 mobiles at once. So I have to send SMS in multiple times. How can I split the parametermobiles like this

http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=11111,22222
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=33333,44444
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=55555,66666....

My current function in php is

function send_sms($message, $numbers) {
        $apikey="xxxxx";
        $message=urlencode($message);
        $var = "apikey=".$apikey."&text=".$message."&mobiles=".$numbers";
        $curl=curl_init('http://smsserviceprovider.com/sendsms.php?'.$var);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
        $response=curl_exec($curl);
        curl_close($curl);
        return $response; 
}
  • 写回答

2条回答 默认 最新

  • doujiu8479 2016-09-11 10:27
    关注

    The trick is to build an array from your comma-separated list of phone numbers, using something like explode() and then group them into chunks of 50 using something like array_chunk(). However, this does mean your function now has to make multiple requests instead of one due to this constraint. So in the following example I return an array of responses instead of just a single response.

    function send_sms($message, $numbers) {
        $responses = [];
        $params = [
            'text'    => $message,
            'mobiles' => '',
            'apikey'  => 'xxxxx',
        ];
    
        $numbers = explode(',', $numbers);
    
        foreach(array_chunk($numbers, 50) as $numlist) {
            $params["mobiles"] = $numlist;
            $queryString = http_build_query($params);
            $responses[] = file_get_contents($url . $queryString);
        }
        return $responses;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题