【问题标题】:Request-URI Too Long - SMS API请求 URI 太长 - SMS API
【发布时间】:2017-07-11 13:53:22
【问题描述】:

我的问题有点奇怪。我从我的提供商那里得到了这个 bulksms 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 的长度超过了此服务器的容量限制。 此外,在尝试使用 ErrorDocument 处理请求时遇到 414 Request-URI Too Long 错误。

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

谢谢

【问题讨论】:

    标签: php apache api curl bulksms


    【解决方案1】:

    您能否尝试让 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);
    

    【讨论】:

    • 请问我该如何使用 POST?谢谢
    • 我试过了,没用。即使对于任何数字,无论是少数还是很多。我的原始代码有效。问题是当数字太多时,它会失败。
    • 这意味着 API 不读取 POST 数据,这很奇怪,因为据我了解,是他们服务于阻止请求?
    • 我认为这是两种可能性之一,第一种:这是 API 限制电话号码数量的方式(一种奇怪的方式)。第二:你通过一个代理,使阻塞
    • 如果这是第一次尝试联系 API 并向他们公开您的问题。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      那么,我必须想办法解决我自己的问题。如果 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);    
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-02
        • 2022-06-15
        • 2017-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        相关资源
        最近更新 更多