0

I was just doing curl requests with Facebook I have faced a problem if I can just cross that problem I can continue everything easily.

My curl code

function curl($url, $data=null, $ua=null, $cookie=null){
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    if($data != null){
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    if($cookie != null){
        curl_setopt($c, CURLOPT_COOKIE, $cookie);
    }
    if($ua != null){
        curl_setopt($c, CURLOPT_USERAGENT, $ua);
    }
    $hmm = curl_exec($c);
    curl_close($c);
    return $hmm;
}
$ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0';
$data = curl('https://facebook.com/', 0, $ua, 0,); //$data stores the html response of Facebook.com
print_r($data);

So from this code we get the html response of facebook.com my problem where I got stuck was getting some values from the html response I need to get the values input fields You can see the view source here :- view-source:https://www.facebook.com So please help me to get the values of input fields from first form (form id="login_form" action="https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=111" method="post" novalidate="1" onsubmit="") Example :- I need to get from this field (input type="hidden" name="jazoest" value="2691" autocomplete="off" /) name and value so I need to echo jazoest,2691 and other input fields like this I have tried the preg_match it is not working as expected and I have an example with Dom does same thing

Use this code with the curl function

$ua = 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)';
$data = curl('https://m.facebook.com/', 0, $ua, 0,); //$data stores the html response of Facebook.com
print_r($data);

This is the mobile web url of Facebook and useragent used here from this with help of Dom we can get the input fields with the code below

function parse_inputs($html) {
    $dom = new DOMDocument;
    @$dom->loadxml($html);
    $inputs = $dom->getElementsByTagName('input');
    return($inputs);
}

$inputs = parse_inputs($data);
    $post_params = "";
    foreach ($inputs as $input) {
                $post_params .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
        }
print_r($post_params);

From this code I can get the input fields for m.facebook.com but not for www.facebook.com please help me with this and another useful example is here please check this also :- https://github.com/jerry-riady/Script-auto-like-face/blob/master/update.php Thanks in advance for all answers.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55

2 Answers2

1

Using loadHTML instead of loadXML should work (tested locally):

Replace:

@$dom->loadxml($html);

with:

$dom->loadHTML($html);

Also, instead of muting errors altogether with @ (very rarely a good idea), I would suggest using the following line (outside of your parse_inputs function):

libxml_use_internal_errors(true);

This is a more appropriate way to mute error output (they'll still be available through libxml_get_last_error() and/or libxml_get_errors() (source).

Jeto
  • 14,596
  • 2
  • 32
  • 46
  • i know that with PHP in general, using @ is rarely a good idea, but unless you're a Facebook developer looking for bugs to fix, or you intend to actually make a bugreport to facebook about mistakes in their HTML, you're just going to ignore the errors in the html anyway, and quickest way of ignoring them is to use @ :) – hanshenrik Apr 21 '19 at 00:23
  • Brother can you suggest a place where can I see all Dom functions in php – Joel Varghese Apr 21 '19 at 00:29
  • @hanshenrik Fair enough, though that function *might* generate errors that aren't related to the HTML structure itself, you never know. [Here's an example](https://3v4l.org/WSLg1) where you pass an array as an argument (a typo, or you forgot that your html form can send an array of strings, or any other reason). All in all, you might miss something important and not related to HTML/XML parsing itself, so if you can avoid it (and in this case you can), I'd say go for it :) – Jeto Apr 21 '19 at 00:29
  • @JoelVarghese https://www.php.net/manual/en/book.dom.php. Also did you try replacing `loadXML` with `loadHTML`? – Jeto Apr 21 '19 at 00:31
  • And your code works perfectly thanks brother and I need an way to extract it from a place to another for example like from to and get this part only as well store this element in a variable ajaxResponseToken:{secret:"cI85PtccdQjsK9yEvp6emzBL11QlLJpT",encrypted:"AYkJqqRg2tM3nta48kXGlLszSuzWZZEHhLK40KFYXhhSzWjw7FtBsifmKDOv0XPtUIw1g9Z0gjMTqtscR6mXGHdfaJc_ScKgqZWK5y1kJ7Accw"} from the view-source:https://m.facebook.com please checkout – Joel Varghese Apr 21 '19 at 00:33
0

the user-agent

Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)

makes facebook believes you're a (nokia) mobile phone and attempts to redirect you to m.facebook.com, even if your request is to https://www.facebook.com .. to fix that, use a desktop user-agent instead, for example

Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0

this code:

<?php
declare(strict_types=1);
$ch=curl_init();
curl_setopt_array($ch,array(
    CURLOPT_URL=>'https://www.facebook.com',
    CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',//'libcurl/'.(curl_version()['version']).' PHP/'.PHP_VERSION,
    CURLOPT_RETURNTRANSFER=>1,
));
$html=curl_exec($ch);
$domd=@DOMDocument::loadHTML($html);
$inputs=[];
foreach($domd->getElementsByTagName("input") as $input){
    $inputs[$input->getAttribute("name")]=$input->getAttribute("value");
}
print_r($inputs);

gives this output:

$ php wtf4.php
Array
(
    [jazoest] => 2747
    [lsd] => AVpUepnL
    [email] =>
    [pass] =>
    [] => Logg inn
    [timezone] =>
    [lgndim] =>
    [lgnrnd] => 171046_tirL
    [lgnjs] => n
    [ab_test_data] =>
    [locale] => nb_NO
    [next] => https://www.facebook.com/
    [login_source] => login_bluebar
    [prefill_contact_point] =>
    [prefill_source] =>
    [prefill_type] =>
    [firstname] =>
    [lastname] =>
    [reg_email__] =>
    [reg_email_confirmation__] =>
    [reg_second_contactpoint__] =>
    [reg_passwd__] =>
    [sex] => 2
    [referrer] =>
    [asked_to_login] => 0
    [terms] => on
    [ns] => 0
    [ri] => a78c3ab6-0e06-e414-b463-452c92229760
    [action_dialog_shown] =>
    [contactpoint_label] => email_or_phone
    [ignore] => reg_email_confirmation__|reg_second_contactpoint__
    [reg_instance] => BrW7XFdd5CvG1L4FidZcVijt
    [captcha_persist_data] => AZl2s6wFOpoQEyjGUhIQJW23Say7yoZA6QdqOYGsPdq52J_eXFWd6lVUIwsHSDDPDyK2dOdsaN8uh2HCO7nKrZawq0lN16Nq0w4qrl-IzIFK9QHic9uSo5kaElwzPLgLnLvRmswIUI_cfils5_0qrhOcCAghdy-wzJmamAi015ksnMSe6ZP3OQHf9l8Hcx1PqIbSK-vlKu9PWDUbSbtXT7o4NFM5jd3gAwPH2fVYhxtsc17DrOE5ABeZ_49j-WBNJNHEiiibndcI2iTVRt1QEMlzVDf5SNfSA0Ht7cbV9cA-u7DjTT8S40Wfw7xdmZ65dVpMR338xQuKuDZ2_sKKNousie9nKdono7COI51BFoHFFaKTq5ntNbaqNNxF3h1YBxE
    [captcha_response] =>
)
  • btw you may find this project interesting: https://github.com/divinity76/msgme

    • command line tool to send messages to facebook, and it does login to facebook to achieve that.
hanshenrik
  • 19,904
  • 4
  • 43
  • 89