如何在PHP中重置Curl变量? [英] How do I reset Curl variables in PHP?
问题描述
我想在一行中做一些Curl调用,第一个是一个帖子,但对于第二个,我只是想加载一个页面,而不是发布任何东西。
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;
当我关闭curl句柄并为第二个请求打开一个新的句柄时,它工作正常。
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?
推荐答案
您可以轻松发出多种不同类型的电话,只要继续呼叫setopt在GET和POST之间切换,并根据需要更改URL:
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);
只需更改所需的OPT,Curl将忽略先前设置的不适用于当前请求(例如,在做get时不要打扰POSTFIELDS,因为它们不会被CURL使用)。
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中重置Curl变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!