Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rico's avatar
Level 1

My request are called twice

I'm using laravel 5.1, I have a problem with my cURL request, the API is called twice.

I tried to execute the same code on a single .php file without laravel and everything is ok.

I see on the web that may come from the mod_rewrite rules because of parameters but I don't know how check that.

My function look like this :

$url = 'http://api.com/api.asmx/deepSearchCreate';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'apikey=API_KEY&search='.strip_tags(trim($article->contenu)).'&source=&searchid=');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json, TRUE);

$id = $array['collection']['@attributes']['id'];

Httacces :

<IfModule mod_headers.c>
    <FilesMatch "\.(eot|font.css|otf|ttc|ttf|woff)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Deny access to framework directories
    RewriteRule ^(app/|bootstrap/|config/|database/|resources/|storage/tests|vendor/) - [R=404,L,NC]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # And dot files/folders (for example .env)
    RedirectMatch 404 /\..*$

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
0 likes
6 replies
kmligue's avatar

Looks like we have the same problem. Were you able to solve it? My scenario i have is that i use curl to scrape site and after getting all the results i save it to my DB. When i check my DB there are duplicate entries. I hope someone could help us here.

ajzo's avatar

I had the same problem today. I was using browser-sync in elixir with a proxy, and in my case it was calling the POST requests twice. When I requested the page without the proxy, everything seems fine. I have no idea why though.

debarshi1993's avatar

Having same problem when using plain curl POST request the function is executing twice as i am saving the request data in the db. So there two sets of data per request

aeadedoyin's avatar

Check that you are not calling $next($request) twice Better yet store in a variable and use in other place in a function.

$nextRequest = $next($request);

2 likes

Please or to participate in this conversation.