2

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

6
  • Can you show the contents of the $html variable? Commented Feb 6, 2012 at 10:55
  • 1
    exactly I don't know but whenever you store curl_exec($ch)in a varible its print data and then second time when you write print ,to avoid it you can do ob_start(); curl_exec($ch); $html = ob_get_contents(); ob_end_clean(); Commented Feb 6, 2012 at 11:02
  • @Poonam, write that up as an answer because I think that will be his solution. It almost seems like $html is getting a reference to the function and rerunning it, but the docs suggest no such possible behavior. Commented Feb 6, 2012 at 11:10
  • @Poonam There is no reason in the current code for the remote URL to be called twice, which is presumably why two records are inserted into the DB. Adding output buffering will not fix the problem, it will simply increase memory usage. I suspect the above script is being called twice and I suspect I know why, which is why I have asked to see the HTML. Commented Feb 6, 2012 at 11:10
  • @HanTimmers I could do with looking at the whole page really, you could post it on pastebin if you don't to put it here. What I will be looking for, amongst other things, are <script> tags with an empty src=, <image> tags with an empty src= and <link> tags with an empty href=. Commented Feb 7, 2012 at 10:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not a bad idea with what the code above does, but it is not going to fix the problem.
This worked for me, thanks! I had a php process executing two times because of that.
this solution not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.