Forum Moderators: coopster

Message Too Old, No Replies

Should I use fsockopen or something else?

if I want to download websites source code

         

strokaz

9:39 am on Jan 22, 2007 (gmt 0)

10+ Year Member



Hello,

Currently I am making a "tool" :) that will grab some websites source code and will search for a specified text (e.g. javascript codes) in it. I am using fsockopen function for connection to a website and it works great if the websites location is normal. E.g. if the website starts at www.example.com then fsockopen works fine, but if the website uses auto-redirect(via Location header field) to www.example.com/en/ or www.example.com/login.php then it doesn work. It only shows the redirect pages headers.

Maybe you have some better ideas how to grab websites source code? I've heard that fopen is not working remotely with most servers..

Any help is very appreciated!

Thanks

barns101

1:05 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



I can't help directly because I've never done something like this, but...

How about using fsockopen() to get your target page and then using a regex to check the headers for a redirect? If a redirect is present then just use fsockopen() again to open the new location.

strokaz

2:02 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



but then if I try to use fsockopen for a, let's say, www.example.com/login.php it throws an error
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /search.php on line XX

as I understand, fsockopen can be used only for connecting to the direct domain (www.example.com) only..

Now I've tried using fopen instead, and with my testing server it works OK, because safe mode is turned off. But it would not work with many more secured servers..

jatar_k

2:24 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try using cURL [php.net] then

strokaz

2:38 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



As I understand, curl must be installed aditionally on the server? It doesn't come with PHP?

barns101

4:14 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



Ah I see. What you could probably do (I've never tried) is to use fsockopen() to connect to www.example.com and then use a GET request to read login.php and play with the data that is returned. Take a look at the code I posted in this thread [webmasterworld.com] and I'm sure that it could be modified for a GET request. :)