简体   繁体   English

使用 curl 登录 Facebook

[英]Login facebook with curl

I need to login facebook with curl.我需要用 curl 登录 Facebook。 I already tested in browser with condition disable cookie, javascript and Opera mini 4 User-agent.我已经在浏览器中测试了条件禁用 cookie、javascript 和 Opera mini 4 用户代理。 It login succefully.它登录成功。 with that i check with inspect what happens within request header and response headers.我检查并检查请求标头和响应标头中发生的情况。 i check it already.我已经检查过了。 the first step facebook will generate 4 random values within hidden type input " lsd, jazoest,m_ts,li ".第一步 facebook 将在隐藏类型输入“ lsd, jazoest,m_ts,li ”中生成 4 个随机值。 the second step after post it will redirect to specific location with session.发布后的第二步将重定向到会话的特定位置。 Cause, i disable cookie.因为,我禁用了 cookie。 so i see facebook switch to session.所以我看到 facebook 切换到会话。 after this step.在这一步之后。 i should see my timeline page.我应该看到我的时间线页面。 but, it nothing happens.但是,它什么也没有发生。 it just post to login page without follow location.它只是发布到登录页面而不关注位置。 object httpConnect is wrapper from curl.对象 httpConnect 是 curl 的包装器。

$headers = [
        'User-agent: Opera/9.80 (J2ME/MIDP; Opera Mini/4.0.10992/35.5561; U; hr) Presto/2.8.119 Version/11.10',
    ];

$facebook = new httpConnect('https://m.facebook.com/');

$facebook->setHeaders($headers);

$facebook->setOptions();

$facebook->sendRequest();

$sourceCode = $facebook->getResponse();

preg_match('/<input type="hidden" name="lsd" value="(.*?)" autocomplete="off" \/>/', $sourceCode, $lsd);

preg_match('/<input type="hidden" name="jazoest" value="(.*?)" autocomplete="off" \/>/', $sourceCode, $jazoest);

preg_match('/<input type="hidden" name="m_ts" value="(.*?)" \/>/', $sourceCode, $m_ts);

preg_match('/<input type="hidden" name="li" value="(.*?)" \/>/', $sourceCode, $li);

$data = [
    'lsd' => $lsd[1],
    'jazoest' => $jazoest[1],
    'm_ts'  => $m_ts[1],
    'li' => $li[1],
    'try_number' => 0,
    'unrecognized_tries' => 0,
    'email' => 'email',
    'pass' => 'password',
    'login' => 'Log In',
    '_fb_noscript' => 'true'
];

$options = [
    CURLOPT_POST            => TRUE,
    CURLOPT_POSTFIELDS      => $data,
    CURLOPT_FOLLOWLOCATION  => TRUE,
];

$facebook = new httpConnect('https://m.facebook.com/login/device-based/regular/login/?refsrc=https%3A%2F%2Fm.facebook.com%2F&amp;lwv=100&amp;refid=8');

$facebook->setHeaders($headers);

$facebook->setOptions($options);

$facebook->sendRequest();

echo $facebook->getResponse();

your login code is not even close to being complete,您的登录代码甚至还没有完成,

first off, the first request to m.facebook.com to get the original cookie requires the accept-language header (seriously, it won't let you login without accept-language, weird shit, here is the 1 i am using: accept-language:en-US,en;q=0.8 ),首先,第一个向 m.facebook.com 请求获取原始 cookie 需要accept-language标头(说真的,它不会让您在没有接受语言的情况下登录,奇怪的狗屎,这是我正在使用的 1: accept-language:en-US,en;q=0.8 ),

second, you're trying to parse HTML with regex, stop doing that , you have to use a proper HTML parser instead, PHP has several HTML parsers (including DOMDocument, XMLReader, XMLParser, SimpleXml,...), third, you're trying to hardcode the login form parameters, don't do that, extract them dynamically, you must fetch all <input> elements that is a descendant of the login_form element dynamically, and add them to your login request, and just fill out "email" and "pass" inputs manually.其次,您正在尝试使用正则表达式解析 HTML,停止这样做,您必须改用适当的 HTML 解析器,PHP有几个 HTML 解析器(包括 DOMDocument、XMLReader、XMLParser、SimpleXml 等),第三,您重新尝试对登录表单参数进行硬编码,不要这样做,动态提取它们,您必须动态获取作为login_form元素后代的所有<input>元素,并将它们添加到您的登录请求中,然后填写“电子邮件”和“通过”手动输入。

next up, sometimes you will randomly get a request to install the facebook mobile app instead after logging in (this doesn't happen every time, but seemingly randomly), when you get this question, you MUST answer either yes or no, facebook will not allow you to continue the login before you answer.接下来,有时您会在登录后随机收到安装 facebook 移动应用程序的请求(这不会每次都发生,但似乎是随机的),当您收到此问题时,您必须回答是或否,facebook 会不允许您在回答之前继续登录。 the question can be detected by looking for an <a> element with a href= that contains the string /login/save-device/cancel/ , and you can answer not interested by doing a simple GET request to the href pointed to by that a-element.可以通过查找带有包含字符串/login/save-device/cancel/href=<a>元素来检测问题,并且您可以通过对指向的 href 执行简单的 GET 请求来回答not interested一个元素。 finally, login success can be verified by looking for an a-element with an href containing /logout.php最后,可以通过查找带有包含/logout.php的 href 的 a 元素来验证登录成功

you can find a sample login code from the FacebookRelay constructor of the msgme project, here: https://github.com/divinity76/msgme/blob/master/src/php/relays/facebook.relay.php您可以在 msgme 项目的 FacebookRelay 构造函数中找到示例登录代码,这里: https : //github.com/divinity76/msgme/blob/master/src/php/relays/facebook.relay.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM