开发者

PHP read from a webpage that requires authentication

开发者 https://www.devze.com 2023-03-05 02:04 出处:网络
I\'m trying to get the content of a webpage that require authentication using PHP. Ideally, I\'d like to use the simple html dom parser: http://simplehtmldom.sourceforge.net.

I'm trying to get the content of a webpage that require authentication using PHP.

Ideally, I'd like to use the simple html dom parser: http://simplehtmldom.sourceforge.net.

Anyone knows of a way to do this?

Edit:

Tried the following code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . urlencode($username) . '&password=' . urlencode($pass));

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_开发者_如何学PythonUSERAGENT, $_SERVER['HTTP_USER_AGENT']);

echo curl_exec($ch);

curl_close($ch);

But now I get a 405 HTTP error.


I've never used that parser, but their sample code makes it seem like it can load data from either a file or a URL. I would use php's curl functions, which easily allow you to access a page with several types of authentication, save the results to a file and then use the library to parse the file.

http://www.php.net/manual/en/book.curl.php

Check out the CURLOPT_HTTPAUTH option specifically.

Hope this helps.

Edit:

I had to look up 405; I've never seen one. It sounds like your ISP doesn't allow POST requests, or possibly doesn't allow them without SSL:

http://www.checkupdown.com/status/E405.html

I would talk to whoever runs your server about the 405. Your code looks good to me. Does posting the login form return the page you want, or are you going to have to pull down another once you have the session info saved?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号