PHP 与cUrl - while 循环中的 POST 问题
我有一个 while 循环,里面有 cUrl 。 (由于各种原因,我无法使用curl_multi。)问题是cUrl似乎在每次循环遍历后保存它发布的数据。例如,如果参数 X 在第一次循环中为 One,如果在第二次循环中为 Two,则 cUrl 会发布:“One,Two”。它应该只是发布“两个”。(尽管关闭并取消设置卷曲句柄。)
这是代码的简化版本(删除了不必要的信息):
<?php
while(true){
// code to form the $url. this code is local to the loop.
// so the variables should be "erased" and made new for each
// loop through.
$ch = curl_init();
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
curl_setopt($ch,CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
curl_close($ch);
unset($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$resultTable = $xpath->evaluate("/html/body//table");
// $resultTable is 20 the first time through the loop,
// and 0 everytime thereafter because the POSTing doesn't work right
//with the "saved" parameters.
我在这里做错了什么?
编辑
迈克尔是对的。我需要取消设置 $fields/$fields_string。新数据被连接到旧数据上。
但我不明白为什么会发生这种情况。 $fields 是循环中的局部变量。 循环结束后是否应该将其从堆栈中删除,然后再创建一个新的?
I have a while loop, with cUrl inside. (I can't use curl_multi for various reasons.) The problem is that cUrl seems to save the data it POSTS after each loop traversal. For instance, if parameter X is One the first loop through, and if it's Two the second loop through, cUrl posts: "One,Two". It should just POST "Two".(This is despite closing and unsetting the curl handle.)
Here's a simplified version of the code (with unecessary info stripped out):
<?php
while(true){
// code to form the $url. this code is local to the loop.
// so the variables should be "erased" and made new for each
// loop through.
$ch = curl_init();
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
curl_setopt($ch,CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
curl_close($ch);
unset($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$resultTable = $xpath->evaluate("/html/body//table");
// $resultTable is 20 the first time through the loop,
// and 0 everytime thereafter because the POSTing doesn't work right
//with the "saved" parameters.
What am I doing wrong here?
EDIT
Michael was right. I needed to unset $fields/$fields_string. The new data was concatenated onto the old data.
I don't understand why this happens, though. $fields is a local variable in the loop. Shouldn't it be removed from the stack once the loop ends, and then it'd be created a new?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过打印 $fields_string 和 $fields 以在每次循环迭代时查看它们的值?也许其中之一还没有被清除。由于您每次都使用curl_init/curl_close,因此问题可能不在于curl 本身。
Have you tried printing the $fields_string and $fields to see their values at each loop iteration? Perhaps one of them isn't getting cleared. Since you're using curl_init/curl_close each time, the problem is likely not in curl itself.