PHP Curl脚本生成了重复请求

4

我有一个基于PHP Curl的函数,用于应用程序中的各种请求。通常使用该函数在同一台机器(本地主机)上与其他服务器通信/触发请求。以下是该函数:

/**
 * @desc If a curl_multi handle is passed, a new curl 
 * instance is added to the handle and the curl id is returned as string.
 * @param string $theServer
 * @param string $thePath
 * @param number $thePort
 * @param curl_multi $mcHandle
 * @return string|curl_handle
 */
function sendPOSTtoURL($theServer, $thePath, $theData, 
                        $thePort = 80, $mcHandle = null) {
    global $bDebug; $theResult = "";    //$bDebug = true;
    $cPost = curl_init();
    if ($cPost !== false)   {
        curl_setopt($cPost, CURLOPT_URL, "http://".$theServer.$thePath);
        if ($thePort != 80) curl_setopt($cPost, CURLOPT_PORT, $thePort);    
            // Set port if different from 80
        curl_setopt($cPost, CURLOPT_HEADER, false);
        curl_setopt($cPost, CURLOPT_FORBID_REUSE, true);
        curl_setopt($cPost, CURLOPT_FRESH_CONNECT, true);
        curl_setopt ($cPost, CURLOPT_POST, true);
        curl_setopt ($cPost, CURLOPT_POSTFIELDS, $theData);
        curl_setopt ($cPost, CURLOPT_RETURNTRANSFER, true);
        $returndata = curl_exec ($cPost);
        if ($mcHandle == null)  {
            if (! $theResult = curl_exec($cPost))   {
                if (curl_errno($cPost) > 0) {   
                                $theResult = CURL_ERROR;    
                                if ($bDebug)    
                                      $theResult .= fcURLError($cPost); 
                            }
                else    {
                                $theResult = CURL_NOERR_NOINFO; 
                                if ($bDebug)
                                      $theResult .= fcURLError($cPost); 
                            }
            }
            curl_close($cPost);
            return $theResult;
        }   else    {
            curl_multi_add_handle($mcHandle, $cPost);
            return $cPost;
        }
    }   else    return CURL_INIT_FAIL;
}

我不会涉及到复杂的内容,但是它旨在返回文本,如果是单个请求则返回文本,如果传递了multi_curl句柄,则附加一个curl句柄。
问题在于,当我请求它在远程服务器上执行任务时,它会发出双重请求。我们有一个设置,其中请求服务器通过VPN连接到远程服务器,并可以通过它对外部互联网进行请求。我以为目标服务器运行了两次,所以我从调用服务器上进行了一次调用(用于发送电子邮件),如下所示:-
    $aEmail = array("email_address" => $sEmailAddress, 
                    "email_subject" => $sEmailSubject, 
                    "email_body" => $sEmailBody);
    echo sendPOSTtoURL("<REMOTE IP HERE", 
                       "/emailService/doMail.php?rand=".mt_rand(), 
                       $aEmail);

调用mt_rand函数生成一个随机数,以便我可以识别每个请求。这是我在远程服务器收到的内容:

<CALLING IP> - - [15/Jun/2011:20:39:57 +0500] "POST /emailService/doMail.php?rand=1551627310 HTTP/1.1" 200 1060 "-" "-"
<CALLING IP> - - [15/Jun/2011:20:40:01 +0500] "POST /emailService/doMail.php?rand=1551627310 HTTP/1.1" 200 1060 "-" "-"

正如您所看到的,同一个请求发送了两次。那么你认为会发生什么?没错,目标邮箱会收到2封电子邮件!由于管理原因,我无法在远程服务器上开发处理此问题的逻辑。


有任何身份验证吗?HTTP基本认证将在初始请求中响应401和身份验证领域,然后当请求经过身份验证时,整个内容都会被发送。这在服务器端可能看起来像2个请求,并且根据它如何处理身份验证,可能实际上会影响应用程序本身。 - Richard J
没有身份验证。就像我说的那样,会生成两封电子邮件。日志还显示HTTP响应200被返回了两次。 - M. Faraz
1个回答

5
您出现了双重调用curl_exec()的问题。
$returndata = curl_exec ($cPost);
    if ($mcHandle == null)  {
        if (! $theResult = curl_exec($cPost))   {

我认为参数$mcHandle始终为空。

我完全瞎了...!非常感谢! 然而,$mcHandle并不总是null。 就像我说的,我们使用一种机制,在外部创建一个curl_multi句柄,并在循环中传递该句柄调用此函数。如果未传递,则为null。 再次感谢! - M. Faraz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接