PHP script to automate login and form submit

2019-02-20 18:32发布

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 should first login with a username/password and then navigate to the URL and submit the form (using dyanamic parameters)

How can I do the same using PHP?

1条回答
孤傲高冷的网名
2楼-- · 2019-02-20 18:59

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.

查看更多
登录 后发表回答