獲取網頁cookie信息文件實現模擬登陸

//獲取cookie信息
    public function get_cookie(){ header("Content-Type: text/html;charset=utf-8"); $url = 'http://signin.aliyun.com/login.htm'; //url地址
        $post = "user_principal_name=***@***.onaliyun.com&password_ims=*****"; //POST數據
        $ch = curl_init($url); //初始化
        curl_setopt($ch,CURLOPT_HEADER,1); //將頭文件的信息做爲數據流輸出
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回獲取的輸出文本流
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //發送POST數據
        $content = curl_exec($ch); //執行curl並賦值給$content
        file_put_contents(dirname(__FILE__).'\cookie.txt',$content); curl_close($ch); //關閉curl
        $this->use_cookie(); }
//利用獲取到的cookie信息模擬登陸
public function use_cookie(){ $cookie=file_get_contents(dirname(__FILE__).'\cookie.txt'); //GetBetween():截取字符串中間的字符(我有在隨筆中單獨分享過此方法)
        $local=$this->GetBetween($cookie,'Location:','Server:'); header("location:$local");  //直接跳轉至登陸成功頁面
    }
相關文章
相關標籤/搜索