简体   繁体   English

php curl 发布两次

[英]php curl post twice

I know this is duplicate question.我知道这是重复的问题。 I tried all solutions from stackoverflow.com but I could not resolve it.我尝试了来自 stackoverflow.com 的所有解决方案,但无法解决。 This is very random behavior.这是非常随机的行为。 When php curl post to destination server api, sometimes same request get posted twice to destination.php curl post到目标服务器 api 时,有时相同的请求会被发布两次到目标。 I checked if source php is refreshed, but php is not refreshed.我检查了源php是否刷新,但php没有刷新。 Another strange thing I noticed that, I get curl output of re-post request only.我注意到的另一件奇怪的事情是,我只得到重新发布请求的curl output I don't get curl output of first original request.我没有得到第一个原始请求的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: \r\n" . $curl_output . "\r\n Http code response: " . $code . "\r\n");

Above mentioned code is general code for curl in all php pages of the project.上面提到的代码是项目所有php页面中curl通用代码。 Later I tried different curl code when I found some solutions on stackoverflow.后来我在stackoverflow上找到了一些解决方案,尝试了不同的curl代码。 Following is new curl code which is also not working sometimes and same random strange behavior happens.以下是新的 curl 代码,它有时也不起作用,并且会发生相同的随机奇怪行为。

$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: \r\n" . $curl_output . "\r\n Http code response: " . $code . "\r\n");
ob_end_flush();

Please note $URL and $postdata is already defined.请注意$URL$postdata已经定义。 SaveMyLog function is used for logging purpose. 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 令牌,这会在每次请求后更改。

from your code i see no reason why it should behave in such strange manner.从您的代码中,我看不出为什么它应该以如此奇怪的方式行事。

So there can be some other problem.所以可能还有其他问题。 If you are calling the script from a browser, make sure the browser calls it just once.如果您从浏览器调用脚本,请确保浏览器只调用一次。

Why does a browser send two requests for the same page when it's refreshed? 为什么浏览器在刷新时会为同一页面发送两个请求?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM