PHP 微博登陸

weixin_34146805發表於2016-07-31

利用微博wap版域名進行登陸

public function loginWeibo()
{
        $url = 'https://passport.weibo.cn/sso/login';
        $headers[] = 'Referer:https://passport.weibo.cn/signin/login';
        $headers[] = 'Host:passport.weibo.cn';
        $this->header[] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) 
                AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
        $data=['username'=>'XXXX',
        'password'=>YYYY','savestate'=>1,'entry'=>'mweibo','ec'=>0];
        $info = $this->curl_post($url,$data,$headers);
        
        $cookie = $this->save_weibo_cookie($info);
        //儲存到檔案
        
        file_put_contents('weibo-cookie.txt',$cookie);
        if($cookie)
        {
            return true;
        }
                else
        {
            return false;
        }
        
        
    }
public function save_weibo_cookie($results) : string
{
        preg_match_all('|Set-Cookie: (.*);|U', $results, $arr);
        $cookies = implode(';', $arr[1]);
        return $cookies;
}

public function curl_post($url,$data,$headers)
{
        $ch = curl_init();

        //設定選項,包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");  
                curl_setopt ( $ch, CURLOPT_POST, 1 );       
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER , $headers);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $data );
        $output = curl_exec($ch);
        //釋放curl控制程式碼
        curl_close($ch);
        //列印獲得的?
        return $output;
}

相關文章