我遇到了一个问题,我真的不知道如何解决。
我想要完成的是转到外部网站并检索网页,然后解析网站并获取它的一些值。唯一的问题是该网站需要登录才能工作。
我认为我可以解决它:
我不太确定如何做的步骤是第2步和第3步,我怎么能用PHP做到这一点?它甚至可能吗?
干杯!
答案 0 :(得分:0)
获得用户凭据后,您可以执行以下操作:
//Assuming $_POST['user'] and $_POST['pass'] hold the credentials for the remote site:
//insert the URL the login form on the remote page posts to
$url = "http://www.example.com/login/"
//Define your cookie file path. Must be accessable/writable by the webserver
$cookie_file_path = "/path/for/storing/the/cookies/mycookie.txt";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
//Note: You'll have to check the remote form for it's field names and set user and password to the input field names
curl_setopt($ch, CURLOPT_POSTFIELDS,
"user=$_POST['user']&password=$_POST['pass']");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$return = curl_exec($ch);
print_r $return;
稍后您只需向您发送Cookiefile和Cookiejar卷曲GET请求。
查看官方cURL页面中的PHP examples。