Logging into another site via php to gather information?

I wanted to find out how to login to another site via PHP… I don’t know the proper term for it, but basically, I want a user to be able to enter their login information for another website on mine, and interact with it through mine.

Best example I can think of is twollow.com. Once logged in to twollow.com, you can follow members on twitter through the site, without leaving twollow. I want to know how this type of thing is done.

Hi,

It’s pretty simple. You should go to the site and look at the login form, you need:

  1. form action
  2. fields for the login/password
  3. any other significant fields, for example hidden fields with the names: “action”, “mode” etc

Then you should submit the same information by script and receive the cookies:


# "submit" form
$fp = @fsockopen($domain_name, $port, $errno, $errstr, 30);
fputs($fp, "POST http://$host$post_url HTTP/1.0\\r\
");
fputs($fp, "Host: $host\\r\
");
# it's used if you have got a cookie
# for example you are log on and try to get a page in background
if (!empty($cook))
   fputs($fp, "Cookie: ".$cook."\\r\
");
fputs($fp, "User-Agent: Mozilla/4.5 [en]\\r\
");
fputs($fp, "Content-Type: $content_type\\r\
");
fputs($fp, "Content-Length: ".strlen($post_str)."\\r\
");
fputs($fp, "\\r\
");
fputs($fp, $post_str."\\r\
\\r\
");

# get the response/cookie
        while (!feof($fp)) {
            $line = fgets($fp,4096);

            if ($header_passed == false && ($line == "\
" || $line == "\\r\
")) {
                $header_passed = true;
                continue;
            }

            if ($header_passed == false) {
                $header_line = explode(": ", $line, 2);
                $header_line[0] = strtoupper($header_line[0]);
                $http_header[$header_line[0]] = chop($header_line[1]);

# here you are receiving the required cookies for the next requests
                if ($header_line[0] == 'SET-COOKIE')
                    array_push($cookies, chop($header_line[1]));
                continue;
            }
            $result .= $line;
        }
        fclose ($fp);

where post_str is something like (according the required params)


login=abcd&password=tmp

As the result you will receive a cookies array, which can be used for the next requests (to receive a page as a logged user). This way any info from any site can be grabbed, parsed and so on.

Example using Zend_Http_Client:

<?php

$client = new Zend_Http_Client();
$client->setCookieJar();
$client->setUri('http://example.com/login.php');
$client->setParameterPost(array(
    'username' => USERNAME,
    'password' => PASSWORD,
));
$response = $client->request('POST');

// $response contains everything you need

echo $response->getBody();




// Now we're logged in, get private area!
$client->setUri('http://example.com/members_only.php');
$response = $client->request('GET');

Yes, BTW look at the completed solution in the manual - with cookie and so on:
23.2.2. Adding Cookies and Using Cookie Persistence
http://framework.zend.com/manual/en/zend.http.client.advanced.html