Joel Varghese
Joel Varghese

Reputation: 1

How to select elements from a html response like this using php?

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.

Upvotes: 0

Views: 232

Answers (2)

Jeto
Jeto

Reputation: 14927

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).

Upvotes: 1

hanshenrik
hanshenrik

Reputation: 21455

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.

Upvotes: 0

Related Questions