如何在 PHP 中重置 Curl 变量?
我想连续进行多次 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以轻松发出多种不同类型的调用,只需不断调用 setopt 在 GET 和 POST 之间切换,并根据需要更改 URL:
只需更改您需要的 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:
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).
在 PHP 5.5 中,您可以使用 curl_reset()
In PHP 5.5 you can use curl_reset()