Ignore some fields with regex

Asked

Viewed 114 times

1

In PHP I’m using preg_match_all to get some data from this form:

<form id="login_form" action="/login/device-based/regular/login/?login_attempt=1&amp;lwv=100" method="post" onsubmit="">
   <input type="hidden" name="jazoest" value="2653" autocomplete="off"><input type="hidden" name="lsd" value="AVq7w05r" autocomplete="off"><input type="hidden" autocomplete="off" id="error_box">
   <div id="loginform">
      <input type="hidden" autocomplete="off" id="display" name="display" value=""><input type="hidden" autocomplete="off" id="enable_profile_selector" name="enable_profile_selector" value="">
      <input type="hidden" autocomplete="off" id="isprivate" name="isprivate" value="">
      <input type="hidden" autocomplete="off" id="legacy_return" name="legacy_return" value="0">
      <input type="hidden" autocomplete="off" id="profile_selector_ids" name="profile_selector_ids" value="">
      <input type="hidden" autocomplete="off" id="return_session" name="return_session" value="">
      <input type="hidden" autocomplete="off" id="skip_api_login" name="skip_api_login" value="">
      <input type="hidden" autocomplete="off" id="signed_next" name="signed_next" value="">
      <input type="hidden" autocomplete="off" id="trynum" name="trynum" value="1">
      <input type="hidden" autocomplete="off" name="timezone" value="180" id="u_0_9">
      <input type="hidden" autocomplete="off" name="lgndim" value="eyJ3IjoxOTIwLCJoIjoxMDgwLCJhdyI6MTkyMCwiYWgiOjEwNDAsImMiOjI0fQ==" id="u_0_a"><input type="hidden" name="lgnrnd" value="062429_kygL">
      <input type="hidden" id="lgnjs" name="lgnjs" value="1554816269">
      <div class="clearfix _5466 _44mg" id="email_container">
         <input type="text" class="inputtext _55r1 inputtext _1kbt inputtext _1kbt" name="email" id="email" tabindex="0" placeholder="Email ou telefone" value="" autofocus="1" aria-label="Email ou telefone">
      </div>
      <div class="clearfix _5466 _44mg">
         <input type="password" class="inputtext _55r1 inputtext _1kbt inputtext _1kbt" name="pass" id="pass" tabindex="0" placeholder="Senha" aria-label="Senha">
      </div>
      <div class="_xkt">
         <button value="1" class="_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy" id="loginbutton" name="login" tabindex="0" type="submit">Entrar</button>
      </div>
      <div id="login_link">
         <div class="_xkt"><a href="https://www.facebook.com/recover/initiate/?ars=facebook_login" id="forgot-password-link" target="">Esqueceu a conta?</a></div>
         <div class="_1rf5"><span class="_1rf8">ou</span></div>
         <div class="_xkt"><a role="button" class="_42ft _4jy0 _4jy6 _4jy2 selected _51sy" href="/r.php?locale=pt_BR&amp;display=page" style="font-size: 14px">Criar nova conta</a></div>
      </div>
   </div>
   <input type="hidden" autocomplete="off" id="prefill_contact_point" name="prefill_contact_point" value="[email protected]"><input type="hidden" autocomplete="off" id="prefill_source" name="prefill_source" value="browser_dropdown"><input type="hidden" autocomplete="off" id="prefill_type" name="prefill_type" value="password"><input type="hidden" autocomplete="off" id="first_prefill_source" name="first_prefill_source" value="browser_dropdown"><input type="hidden" autocomplete="off" id="first_prefill_type" name="first_prefill_type" value="contact_point"><input type="hidden" autocomplete="off" id="had_cp_prefilled" name="had_cp_prefilled" value="true"><input type="hidden" autocomplete="off" id="had_password_prefilled" name="had_password_prefilled" value="true"><input type="hidden" autocomplete="off" name="ab_test_data" value="">
</form>

Turns out when I match mine arrays a button appears

ou
Criar nova conta
Esqueceu a conta?

Follow the image:

inserir a descrição da imagem aqui

I just need this:

  public 'jazoest' => string '2729' (length=4)
  public 'lsd' => string 'AVqQhOsV' (length=8)
  public 'display' => string '' (length=0)
  public 'enable_profile_selector' => string '' (length=0)
  public 'isprivate' => string '' (length=0)
  public 'legacy_return' => string '0' (length=1)
  public 'profile_selector_ids' => string '' (length=0)
  public 'return_session' => string '' (length=0)
  public 'skip_api_login' => string '' (length=0)
  public 'signed_next' => string '' (length=0)
  public 'trynum' => string '1' (length=1)
  public 'timezone' => string '' (length=0)
  public 'lgndim' => string '' (length=0)
  public 'lgnrnd' => string '063516__LZq' (length=11)
  public 'lgnjs' => string 'n' (length=1)

The last two objects as email and pass don’t need.

My code is like this:

public function getInputs()
    {
        $options = [
            CURLOPT_URL                         => $this->url['login'],
            CURLOPT_HEADER                  => true,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_USERAGENT               => $this->ua
        ];

        $request = curl_init();

        curl_setopt_array($request, $options);

        $response = curl_exec($request);

        curl_close($request);

        preg_match_all('/name="(.*?)" value="(.*?)"/', $response, $inputs);

        return (object) array_combine($inputs[1], $inputs[2]);
    }
  • 2

    I find it simpler to use DOMDocument, as I have already suggested in your another question. Or else vc traverses the resulting array (the result of array_combine) and removes the elements you don’t need. Use regex (as I explained in another answer) is not always the best solution for manipulating HTML :-)

  • Yes, but I’m familiar with Curl. :(

  • 1

    But in the end the $response will have a string with HTML - and if use CURLOPT_HEADER => false, she will only have HTML, without the headers, which you can pass directly to DOMDocument::loadHTML hassle-free..

  • 1

    Got it now, I disabled the Curl header, and got it. they should translate the Curl documentation :(

  • 1

    There is even documentation in Portuguese, but not everything is 100% well translated. A page of curl_exec, for example, it only has a small part in Portuguese and the rest in English :-(

  • 1

    Oloco.... I didn’t even know I had the documentation on the php.net website, I looked here https://curl.haxx.se/... but vlw brother

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.