php: get content of a protected page?
I am trying to get the html code of a protected page. I am aiming to restyle this page with css, so I need tho get the html code first !!!
I have a valid username and password.
I have tried to use cURL, but I always end up with this message: "the stub received bad data"
the url of the page is开发者_StackOverflow: http://student.guc.edu.eg
Do you have any code already? You need to use code such as this, utilising CURLOPT_HTTPAUTH
and CURLOPT_USERPWD
specifically.
$username = 'studentid';
$password = 'studentpassword';
$ch = curl_init("http://student.guc.edu.eg/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$html = curl_exec($ch);
curl_close($ch);
CURLOPT_HTTPAUTH
The HTTP authentication method(s) to use. The options are: CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE.
The bitwise | (or) operator can be used to combine more than one method. If this is done, cURL will poll the server to see what methods it supports and pick the best one.
CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.
CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.
CURLOPT_USERPWD
A username and password formatted as "[username]:[password]" to use for the connection.
Looking at the headers returned from the site http://student.guc.edu.eg/, it's as follows:
> curl -I http://student.guc.edu.eg/
HTTP/1.1 401 Access Denied
Server: Microsoft-IIS/5.0
Date: Thu, 21 Jul 2011 08:18:34 GMT
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="student.guc.edu.eg"
Connection: close
Content-Length: 4431
Content-Type: text/html
This means that rather than using CURLAUTH_BASIC
, you should try CURLAUTH_NTLM
, and see if that helps.
Don't use cURL, use a class someone else has written which takes away all the hassle of remembering to send the correct headers, producing useful error messages etc.., such as Zend_Http_Client:
http://framework.zend.com/manual/en/zend.http.html
精彩评论