打印返回字符串时,php cURL脚本运行两次

时间:2012-02-06 10:49:42

标签: php curl

我有以下脚本。在远程php脚本中,记录被添加到数据库表中。 当我在这个脚本中留下最后一行(print $ html;)时,会添加2条记录!

当我遗漏该行时,只添加一条记录。但显然我没有任何输出。

如果我将输出写入文件,则只添加一条记录。输出是一个html页面。

<?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;
?>

有什么建议吗?

的Gr。韩

/ * *更新

<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> 

这是$ html的片段,整个页面相当大。 打印html的子字符串表明该脚本将在第二次运行'

@Poonam:当我再次在ob_clean_end()之后打印$ html时,会添加第二条记录。

目前我已经实施了一个非常粗略的解决方法。由于记录具有时间戳,因此如果最后一个记录不是至少1秒,则阻止添加第二个记录。我恨它,但现在它有效。

的Gr。韩

/ * *更新

这个问题不是来自cURL,尝试使用file_get_contents做同样的事情。

也许原因在我正在使用的mod_rewrite中。

的Gr。韩

/ * *更新

最有可能是mod_rewrite。使用直接URL而不是通过重写规则时,它的行为与预期一致。

这些是我正在使用的重写规则:

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

Parts.php是上面发布的脚本。

的Gr。韩

1 个答案:

答案 0 :(得分:1)

将CURLOPT_RETURNTRANSFER设置为false,然后执行curl_exec($ ch)而不是$ html = curl_exec($ ch)。然后curl输出将直接返回给浏览器。