php-mod/curl

Curl executing twice

Closed this issue · 1 comments

srakl commented

I'm using curl in my stripe webhook. When a payment if complete, my function will run a curl to pull data from an api URL. here is my curl code

$curl = new Curl();
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, (YII_ENV == 'dev') ? false : true);
$curl->setOpt(CURLOPT_SSL_VERIFYHOST, (YII_ENV == 'dev') ? false : 2);
//$curl->setOpt(CURLOPT_RETURNTRANSFER, false);
//$curl->setOpt(CURLOPT_URL, $apiURL);
$curl->get($apiURL);

if ($curl->error) {
    Yii::error($curl->errorMessage);
    die;
} else {
    $response = $curl->response;
}

In general the code works fine, but once in a while it will pull data from $apiURL twice. Any idea how to fix this, or prevent it? Thanks.

nadar commented

Hi @srakl

I don't think its a problem of the this CURL library. It must be something around your application. Is this action callable by the web? If its a job, use mutex to ensure its only running once.

  1. Maybe log the $apiURL before the request, so you can trace that in your YII Application: Yii::debug($apiURL, __METHOD__) so you can see in the log files the api has been called.
  2. I would invert the error behavior, check for http 200 success status with $curl->isSuccess() instead of $curl->error.
  3. Don't use die() use the Framework agnostic method to end the application, in your case its Yii::$app->end().