php里有相当于Asp.Net中 CookieContainer的东西吗?
在Asp.Net中,访问页面可通过下面的函数来完成,postData是需要Post指定url的数据:
public string getHtml(string url, CookieContainer cookie, byte[] postData);
在php中如何实现呢?是不是可以这样写呢?
<br /><?php<br />function getHtml($url,$tmpFile,$postData)<br />{<br /> $ch = <em style="color:transparent">本@文来源[email protected]搞@^&代*@码网(</em><q>搞代gaodaima码</q>curl_init($url);<br /> curl_setopt($ch,CURLOPT_HEADER,0);<br /> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <br /> curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpFile);<br /> curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpFile); <br /> if($postData!=NULL)<br /> {<br /> curl_setopt($ch, CURLOPT_POST, 1); <br /> curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); <br /> }<br /> $content=curl_exec($ch); <br /> curl_close($ch); <br /> return $content;<br />}<br />?>
下面这两句是否都不能少呢?
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpFile);
只能通过临时文件吗?还有其他改进的方法吗?O(∩_∩)O谢谢啦~~~~~~
——解决方案——————–
CURLOPT_COOKIEJAR 用于连接关闭时保存cookie到文件
CURLOPT_COOKIEFILE 用于连接时将文件中的cookie发送出去
CURLOPT_COOKIE 用于连接时将变量中的cookie发送出去
是否都要使用,取决于你的应用场景
保存到文件是默认动作
你可以通过设定 CURLOPT_READFUNCTION 用自己的函数来处理