请求 URI 太长 - 短信 API



我的问题有点奇怪。我有来自我的提供商的这个批量 api:

http://www.estoresms.com/smsapi.php?username=user&password=1234&sender=@@sender@@&recipient=@@recipient@@&m
essage=@@message@@&

然后我用PHP包装它并将其传递到cURL中:

$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers."&message=".$text."&";
function curl_get_contents($url)
{   
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$send_it =  curl_get_contents($api);

通常,它工作正常,但是当$recepient(电话号码(超过 300 时,我会收到一个错误:

请求 URI 太长 请求的 URL 长度超过此服务器的容量限制。 此外,尝试使用错误文档处理请求时遇到 414 请求 URI 太长错误。

但是BulkSMS应该能够一次发送到数千个号码。 从我的研究中,我发现URL是有限制的。我不是服务器所有者。我正在制定共享主机计划。请问我如何解决这个问题。我知道有一个解决方案,并不意味着购买我自己的服务器。

谢谢

你能尝试让API使用POST而不是GET吗?它将解决问题。

编辑:

我不确定您的 API 检查 POST,但请尝试:

$api = "http://www.estoresms.com/smsapi.php";
$data = array('username' => $sms_user, 'password' => $sms_pwd, 'sender' => $sender_id , 'recipient' => $numbers , 'message' => $text);
function curl_get_contents($url)
{   
$ch = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$send_it =  curl_get_contents($api);

看看这个代码示例(来自 bulksms.com(。

http://developer.bulksms.com/eapi/code-samples/php/send_sms/

因此,我必须找到解决自己问题的方法。 如果 API 不允许一次使用数千个数字,那么让我们在执行时将其分解为块。

function curl_get_contents($url)
{   
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$how_many = count(explode(',',  $numbers));
if ($how_many > 250){
$swi = range(0, ceil($how_many/250)-1); 
foreach ($swi as $sw){$numbers_a = implode(',', (array_slice(explode(',', $numbers), $sw*250, 250)));
$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers_a."&message=".$text."&";

$send_it =  curl_get_contents($api);
}
}
if ($how_many <= 250){
$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers."&message=".$text."&";
$send_it =  curl_get_contents($api);    
}