Rumah >pembangunan bahagian belakang >tutorial php >PHP CURL模拟登录保存Cookie
一、定义Cookie存储路径
必须使用绝对路径
$cookie_jar = dirname(__FILE__)."/pic.cookie";
二、获取Cookie
将cookie存入文件<p>$url = "http://1.2.3.4/";2.$ch = curl_init();</p><p>curl_setopt($ch, CURLOPT_URL, $url);</p><p>curl_setopt($ch, CURLOPT_HEADER, 0);</p><p>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</p><p>curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);</p><p>$content = curl_exec($ch);</p><p>curl_close($ch);</p>
<p>$ch = curl_init();</p><p>curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305');</p><p>curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);</p><p>curl_setopt($ch, CURLOPT_HEADER, 0);</p><p>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);</p><p>$ret = curl_exec($ch);7.curl_close($ch);</p><p>四、POST提交
<p>$post ="name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7";</p><p>$ch = curl_init();</p><p>curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/loginstudent.action");</p><p>curl_setopt($ch, CURLOPT_HEADER, false);</p><p>curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);</p><p>curl_setopt($ch, CURLOPT_POSTFIELDS, $post);</p><p>curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);</p><p>$result=curl_exec($ch);09.curl_close($ch);</p>五、到指定页面获取数据
<p>$ch = curl_init();</p><p>curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action");</p><p>curl_setopt($ch, CURLOPT_HEADER, false);</p><p>curl_setopt($ch, CURLOPT_HEADER, 0);</p><p>curl_setopt($ch, CURLOPT_RETURNTRANSFER,0); </p><p>curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);</p><p>$html=curl_exec($ch);</p><p>// var_dump($html);</p><p>curl_close($ch);</p>