我有一个基于PHP Curl的函数,用于应用程序中的各种请求。该功能通常用于在同一台机器(localhost)上的其他服务器上通信/触发请求。功能如下: -
/**
* @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连接到远程服务器,并可以通过它向外部Internet发出请求。我以为目标服务器运行了两次,所以我从呼叫服务器拨打电话(用于发送电子邮件),如下所示: -
$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封电子邮件发送给目标!我不能(由于管理[S @#$]原因)开发逻辑来处理远程服务器上的这个。
答案 0 :(得分:5)
你有双重调用curl_exec()
$returndata = curl_exec ($cPost);
if ($mcHandle == null) {
if (! $theResult = curl_exec($cPost)) {
我认为参数$ mcHandle始终为null。