PHP script to automate login and form submit

前端 未结 1 888
梦毁少年i
梦毁少年i 2021-01-16 07:20

I have an external site which requires me to a. login b. post form (with 2-3 dyanamic parameters)

I need a PHP script to automate this behavior. i.e. the script shou

1条回答
  •  感动是毒
    2021-01-16 08:14

    I recommend using this class:

    http://semlabs.co.uk/journal/object-oriented-curl-class-with-multi-threading

    It will be something like this:

    $c = new CURLRequest();
    $c->retry = 2;
    $c->get( $url, $this->curlOpts );
    $url = 'https://secure.login.co.uk/';
    $opts = array(
        CURLOPT_USERAGENT       => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        CURLOPT_COOKIEFILE      => 'anc.tmp',
        CURLOPT_COOKIEJAR       => 'anc.tmp',
        CURLOPT_FOLLOWLOCATION  => 1,
        CURLOPT_RETURNTRANSFER  => 1,
        CURLOPT_SSL_VERIFYHOST  => 0,
        CURLOPT_SSL_VERIFYPEER  => 0,
        CURLOPT_TIMEOUT         => 120
    );
    $opts[CURLOPT_POSTFIELDS] = 'username=user&password=pass&submit=1';
    $request = $c->get( $url, $opts );
    

    N.B. Some sites require you to download the login page first to set a cookie.

    Also, you need to url_encode special chars in the post fields.

    0 讨论(0)
提交回复
热议问题