faqts : Computers : Programming : Languages : PHP : Not Quite PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 30 people (83%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Can I write PHP code to interact with a DIFFERENT server as if php were a browser? Cookies and all?

Mar 28th, 2008 17:45
ha mo, Rick Morris, Ben Udall, Lloyd Parl,


I believe it is possible to create such a script.  However, a good bit 
of work and research would have to be done.  Below is a short script to 
retrieve a webpage.
$host    = 'www.faqts.com';
$port    = 80;
$page    = '/index.phtml';
$timeout = 30;
// Open a socket connection to the host
$fp = fsockopen($host, $port, &$err_num, &$err_msg, $timeout);
if ($fp)
{
    // Send request for the page
    fputs($fp, "GET $page HTTP/1.0\r\n");
    // Get the response
    $response = '';
    while (!feof($fp))
        $response .= fgets($fp, 128);
}
else
{
    print("ERROR #$err_num: $err_msg<BR>\n");
}
If there are no errors, $response will now contain everything a browser
would receive if accessing that page.  Hopefully, this is enough to get 
started.
(Added note) PHP can be configured to work with an external "user agent
emulation" utility known as cURL (http://curl.haxx.se), which has the
ability to masquerade as any browser, POST data to form handlers, accept
cookies, etc... as well as handle other protocols, such as FTP, Telnet,
SSL, etc...
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com