PHP卷发两次

  • 本文关键字:两次 PHP php curl
  • 更新时间 :
  • 英文 :


我知道这是重复的问题。我尝试了stackoverflow.com的所有解决方案,但无法解决。这是非常随机的行为。当php curl post到目标服务器API时,有时相同的请求将两次发布到目的地。我检查了源php是否刷新,但是php未刷新。我注意到的另一件奇怪的事情是,我仅收到重新启动请求的curl output。我没有第一个原始请求的curl output

$curl_unit = curl_init($URL);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_unit, CURLOPT_POST, 1);
curl_setopt($curl_unit, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_unit, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_unit, CURLOPT_INTERFACE, gethostbyname($_SERVER['HTTP_HOST']));
curl_setopt($curl_unit, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl_unit, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$curl_output = curl_exec($curl_unit);
$code = curl_getinfo($curl_unit, CURLINFO_HTTP_CODE);
curl_close($curl_unit);
SaveMyLog("DATA RECEIVED FROM destination: rn" . $curl_output . "rn Http code response: " . $code . "rn");

上面提到的代码是项目的所有phpcurl的常规代码。后来,当我在Stackoverflow上找到一些解决方案时,我尝试了不同的卷曲代码。以下是新的卷曲代码,有时也无法正常工作,并且发生同样的随机奇怪行为。

$curl_unit = curl_init($URL);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_unit, CURLOPT_POST, 1);
curl_setopt($curl_unit, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl_unit, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_unit, CURLOPT_INTERFACE, gethostbyname($_SERVER['HTTP_HOST']));
curl_setopt($curl_unit, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl_unit, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
ob_start(); 
curl_exec($curl_unit);
$code = curl_getinfo($curl_unit, CURLINFO_HTTP_CODE);
curl_close($curl_unit);
$curl_output = ob_get_contents(); 
ob_end_clean();         
SaveMyLog("DATA RECEIVED FROM destination: rn" . $curl_output . "rn Http code response: " . $code . "rn");
ob_end_flush();

请注意$URL$postdata已经定义。 SaveMyLog功能用于记录目的。

function SaveMyLog($lin, $deprecated = 'mylog.log')
{
    $logid = '';
    $date = getdate();
    $fileName = basename($_SERVER['SCRIPT_FILENAME']);
    $file = str_replace('.php', '', $fileName);
    $logfile = $file . "_" . $date['year'] . "-" . $date['mon'] . "-" . $date['mday'] . ".log";
    $fd = fopen('./logs/application_logs/' . $logfile, 'a+');
    fwrite($fd, date('Y-m-d H:i:s') . $logid . "t" . $lin . "n");
    fclose($fd);
    @chmod('../logs/application_logs/' . $logfile, 0666);
}

您可以尝试使用CSRF令牌,这将在每个请求后都会更改。

从您的代码中,我没有理由以这种奇怪的方式行事。

因此可能还有其他问题。如果您是从浏览器调用脚本,请确保浏览器仅调用一次。

为什么浏览器在刷新时向同一页面发送两个请求?

我们遇到了相同的问题,我们通过禁用或卸载Firebug Lite扩展名来解决它。

最新更新