問題描述
我有一個外部網(wǎng)站,需要我一個.登錄灣post表單(帶有2-3個動態(tài)參數(shù))
I have an external site which requires me to a. login b. post form (with 2-3 dyanamic parameters)
我需要一個 PHP 腳本來自動執(zhí)行此行為.即腳本應首先使用用戶名/密碼登錄,然后導航到 URL 并提交表單(使用動態(tài)參數(shù))
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)
如何使用 PHP 執(zhí)行相同的操作?
How can I do the same using PHP?
推薦答案
我推薦使用這個類:
http://semlabs.co.uk/日志/面向對象的curl-class-with-multi-threading
它會是這樣的:
$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 );
注意有些網(wǎng)站要求您先下載登錄頁面以設置 cookie.
N.B. Some sites require you to download the login page first to set a cookie.
此外,您需要對帖子字段中的特殊字符進行 url_encode.
Also, you need to url_encode special chars in the post fields.
這篇關于用于自動登錄和表單提交的 PHP 腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!