I know this is duplicate question. I tried all solutions from stackoverflow.com but I could not resolve it.
This is very random behavior. When php curl post
to destination server api, sometimes same request get posted twice to destination. I checked if source php
is refreshed, but php
is not refreshed. Another strange thing I noticed that, I get curl output
of re-post request only. I don't get curl output
of first original request.
$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.
Later I tried different curl code when I found some solutions on stackoverflow. Following is new curl code which is also not working sometimes and same random strange behavior happens.
$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.
SaveMyLog
function is used for logging purpose.
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);
}