是的,是的,我知道用户和密码。
我需要在php中使用一些技巧来登录网站并检索一些图像/内容,就像普通网站一样。
显然有一个curl o file_get_contents它不起作用,因为我没有经过身份验证。
我该怎么做?
身份验证是POST的正常HTTP身份验证。
修改:好的,谢谢您的帮助!
我在这里发布工作代码以供将来参考
//login and set cookie
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile"); # SAME cookiefile
curl_setopt($curl, CURLOPT_URL, "url in which there is the login form");
curl_setopt($curl, CURLOPT_POSTFIELDS, "user=test&password=test&someparam=somevalue"); //put here the post/get values
$output = curl_exec($curl);
echo $output;
//finally fetch my content
curl_setopt($curl, CURLOPT_URL, $url_to_fetch);
$output = curl_exec($curl);
echo $output;
curl_close ($curl);
答案 0 :(得分:1)
使用浏览器对自己进行身份验证,导出Cookie并通过curl使用它们。 在会话持续之前,您应该冒充用户。
我很着急,现在无法为您提供代码,但我认为此指示可以帮助您
您可以使用CURLOPT_COOKIEFILE选项指定存储Cookie的文件。
如php manual中所述:
The name of the file containing the cookie data.
The cookie file can be in Netscape format, or just
plain HTTP-style headers dumped into a file.
答案 1 :(得分:1)
您可以使用curl进行身份验证。 Curl允许将POST变量发送到登录,以及基本的HTTP身份验证。