在Asp.Net中,访问页面可通过下面的函数来完成,postData是需要Post指定url的数据:
public string getHtml(string url, CookieContainer cookie, byte[] postData);
在php中如何实现呢?是不是可以这样写呢?
<?phpfunction getHtml($url,$tmpFile,$postData){ $ch = curl_init($url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpFile); curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpFile); if($postData!=NULL) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); } $content=curl_exec($ch); curl_close($ch); return $content;}?>
CURLOPT_COOKIEJAR 用于连接关闭时保存cookie到文件
CURLOPT_COOKIEFILE 用于连接时将文件中的cookie发送出去
CURLOPT_COOKIE 用于连接时将变量中的cookie发送出去
是否都要使用,取决于你的应用场景
保存到文件是默认动作
你可以通过设定 CURLOPT_READFUNCTION 用自己的函数来处理
多谢版主指导~~~
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号