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

时间:2013-03-07 12:20:03

标签: php curl

我有一个PHP脚本,它使用cURL将XML有效负载发送到SMS服务提供商。问题是用户抱怨他们正在接收多条短信'(有时候全天都有50条),我不知道我的代码或短信服务提供商是否有问题。提交此表单的表单只能由不会多次按提交的人访问。

这是我的代码,如果有人可以快速浏览一下,看看我是否错过了一个重要的选项,如果遗漏,会发送多个请求,我将非常感激:

<?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
}
?>

1 个答案:

答案 0 :(得分:0)

据我所知,没有代码可以请求两次。

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

如何发送两次 - 尝试在此处添加日志

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

在这里

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

将包括当前时间,它将帮助您查看我们在这部分脚本中使用了多少次。

相关问题