如何在 PHP 中重置 Curl 变量?

发布于 2024-12-03 23:27:24 字数 1012 浏览 0 评论 0原文

我想连续进行多次 Curl 调用,第一个是帖子,但对于第二个,我只想加载页面而不发布任何要做的事情。

这是我的代码,它不起作用

$url = 'http://www.xxxx.com/results.php';
$curl_handle=curl_init();
curl_setopt ($curl_handle, CURLOPT_PROXY, $tor);
curl_setopt( $curl_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($curl_handle, CURLOPT_REFERER, $referer);

curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 
$data = 'Manufacturer=1265';
curl_setopt($curl_handle, CURLOPT_POST,1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS ,$data);
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);

$dest = 'http://www.xxxx.com/search.php';
curl_setopt($curl_handle, CURLOPT_GET, 1);
curl_setopt($curl_handle, CURLOPT_URL, $dest);
$result = curl_exec ($curl_handle);
curl_close ($curl_handle); 
echo $result;

当我关闭卷曲句柄并为第二个请求打开一个新句柄时,它工作正常。我认为这不是最佳实践?

I want to do a number of Curl calls in a row, the first one is a post, but for the second one I just want to load a page and not post anything to do.

Here is my code, which does not work

$url = 'http://www.xxxx.com/results.php';
$curl_handle=curl_init();
curl_setopt ($curl_handle, CURLOPT_PROXY, $tor);
curl_setopt( $curl_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($curl_handle, CURLOPT_REFERER, $referer);

curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 
$data = 'Manufacturer=1265';
curl_setopt($curl_handle, CURLOPT_POST,1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS ,$data);
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);

$dest = 'http://www.xxxx.com/search.php';
curl_setopt($curl_handle, CURLOPT_GET, 1);
curl_setopt($curl_handle, CURLOPT_URL, $dest);
$result = curl_exec ($curl_handle);
curl_close ($curl_handle); 
echo $result;

When I close the curl handle and open a new one for the second request it works fine. I do not think this is best practice though?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-12-10 23:27:24

您可以轻松发出多种不同类型的调用,只需不断调用 setopt 在 GET 和 POST 之间切换,并根据需要更改 URL:

... your code up to the exec()...

curl_setopt($curl_handle, CURLOPT_HTTPGET, 1);
curl_setopt($curl_handle, CURLOPT_URL, 'http://....';
$buffer = curl_exec($curl_handle);

curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_URL, 'http://....';
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(...));
$buffer = curl_exec($curl_handle);

只需更改您需要的 OPT。 Curl 将忽略先前设置的不适用于当前请求的内容(例如,在执行 get 时不必费心清除 POSTFIELDS,因为它们无论如何都不会被 CURL 使用)。

You can issue multiple different types of calls easily, just keep calling setopt to switch between GET and POST, and change the URL as needed:

... your code up to the exec()...

curl_setopt($curl_handle, CURLOPT_HTTPGET, 1);
curl_setopt($curl_handle, CURLOPT_URL, 'http://....';
$buffer = curl_exec($curl_handle);

curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_URL, 'http://....';
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(...));
$buffer = curl_exec($curl_handle);

Just change the OPTs you need to. Curl will ignore previously set ones that don't apply to the current request (e.g. don't bother clearing POSTFIELDS while doing a get, because they won't get used by CURL anyways).

孤者何惧 2024-12-10 23:27:24

在 PHP 5.5 中,您可以使用 curl_reset()

In PHP 5.5 you can use curl_reset()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文