Han Timmers
Han Timmers

Reputation: 35

php cURL script run twice when printing return string

I have the following script. In the remote php script a record is added to a database table. When I leave the last line (print $html;) in this script, 2 records are added!

When I leave out that line only one record is added. But then I don't have any output, obviously.

If I write the output to a file, only one record is added. The output is a html page.

<?php
  $ch = curl_init();

  // set URL and other appropriate options
  curl_setopt($ch, CURLOPT_URL, 'http://somedomain.nl/some.php?PARAMS=blabla');
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  // cookie settings
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'some.txt');
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'some.txt');

  // set data to be posted
  curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

  // perform the request
  $html = curl_exec($ch);

  // close the connection
  curl_close($ch);

  print $html;
?>

Any suggestions?

Gr. Han

/** Update

<select class="p_ssyskey_mke" name=P_SSYSKEY_MKE onchange="MerkSubmitP()">
  <option value="">selecteer een merk</option>
  <option value="A0001E2Q">Subaru</option>
  <option selected value="A0001E2S">Toyota</option>
  <option value="A0001E2T">Volkswagen</option>
</select> 

This is a snippet of $html, the entire page is rather large. Printing a substring of the html reveals that the script will run a second time when the '

@Poonam: When I print $html after ob_clean_end() again a second record is added.

For now I have implemented a very crude workaround. Since the record has a timestamp I prevent a second record to be added if the last one is not at least 1 second old. I hate it, but for now it works.

Gr. Han

/** Update

This problem doesn't come from cURL, trying the same thing with file_get_contents does the same thing.

Maybe the cause is in the mod_rewrite I'm using.

Gr. Han

/** Update

Most probably the mod_rewrite. When using direct URL's instead of going through the rewrite-rules it behaves as expected.

these is the rewrite rules I'm using:

RewriteEngine On
RewriteRule ^zoek/(.*)$ parts.php?PARAMS=$1 [L]

Parts.php is the script posted above.

Gr. Han

Upvotes: 2

Views: 4150

Answers (1)

Goeran
Goeran

Reputation: 362

Set CURLOPT_RETURNTRANSFER to false and just do curl_exec($ch) instead of $html = curl_exec($ch). Then the curl output will be directly returned to the browser.

Upvotes: 2

Related Questions