如何在PHP中重置Curl变量?

时间:2011-09-12 18:24:27

标签: 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;

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

2 个答案:

答案 0 :(得分:5)

您可以轻松发出多种不同类型的呼叫,只需继续调用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使用)。

答案 1 :(得分:3)

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