cURL - 发送多个不需要的请求的脚本

问题描述:

我有一个PHP脚本,它使用cURL将XML负载发送给SMS服务提供者。问题在于用户抱怨说他们收到了多条短信“(全天有时候有50条短信),我不知道这是我的代码还是短信服务提供商的问题。提交此表单的表单只能由不会多次按下提交的用户访问。cURL - 发送多个不需要的请求的脚本

这是我的代码,我将非常感激,如果有人能够有一个快速过目,看看是否我缺少一个重要的选择,如果丢失,会发送多个请求:

<?php 
// the following code is only executed if there is no sms timestamp in the db for the user. sms timestamp is added to the db when $error != true; 
$error = false; 

define('XML_PAYLOAD', '<xml data...>'); 
define('XML_POST_URL', 'https://URLOFSERVICEPROVIDER'); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, XML_POST_URL); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
curl_setopt($ch, CURLOPT_TIMEOUT,  5); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_POST,   true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); 
curl_setopt($ch, CURLOPT_HTTPHEADER,  array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen(XML_PAYLOAD))); 
$result = curl_exec($ch); 

if (curl_errno($ch)) { 
    $error = true; 
} else { 
    $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    switch($returnCode){ 
     case 200: 
      break; 
     default: 
      $error = true; 
      break; 
    } 
} 
curl_close($ch); 
if($error == false){ 
    // store that SMS was sent successfully 
} 
?> 
+0

我觉得你的超时时间很短。您可以通过增加至少30秒来检查它,因为在计算上传验证和更多事情时需要花费一些时间 – Vineet1982 2013-03-07 12:23:31

+1

我在代码中没有看到任何检查,您检查SMS是否已经发送或不。用户可能会多次按下发送按钮,因为他们不耐烦。 – hakre 2013-03-07 12:25:13

+0

@hakre对不起,我检查,但没有添加编码。当'$ error'为false时,我将时间戳存储在数据库中。我发布的代码仅在未找到时间戳时执行。 – SammyBlackBaron 2013-03-07 14:18:21

为就我所见,没有代码,它会请求两次。

只有一个问题 - 我没有看到你的代码中的curl_exec,它可以吗?

什么有关发送两次 - 试图在这里添加日志

if (curl_errno($ch)) { 
    $error = true; 
} 

这里

if($error == false){ 
    // store that SMS was sent successfully 
} 

,其中将包括当前的时间,它会帮你看看,多少次我们沃尔特这个脚本的一部分。