PHP 微博登陆

利用微博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;
}

你可能感兴趣的:(PHP 微博登陆)