How to make Php login to different sites?

starhu
starhu used Ask the Experts™
on
Hello,

My customer wants to fill a table with site, login name, password data (many rows).

Our php should start once a day and log in to each site, retrieving some data (counting some words on pages).

How can I log in with php to another site? (knowing the user name and password)

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what do you mean by different sites, yahoo,gmail and so on.
Or your own websites beside this one.

for yahoo,gmail use OpenId

OpenID is a decentralized authentication protocol that makes it easy for people to sign up and access web accounts.
Yahoo, Google and facebook are all OpenID providers, so simply implementing it on your site will be enough for your users to be able to login using them (and any other OpenID provider).
Most Valuable Expert 2011
Top Expert 2016

Commented:
How can I log in with php to another site? (knowing the user name and password)
Expect that each site will be different, so your script design will use a polymorphic adapter pattern.  

Your PHP script will make a GET request for the login page, and will retrieve the HTML.  The script will parse the HTML, isolating the form elements and filling in the name and password.  Then the script will make a POST request, returning the filled-in form.  

For this to work correctly, your script will have to act as a well-behaved web client, accepting and returning cookies, following redirection headers, etc.  It's not a task for the faint of heart, and debugging information is very hard to come by.  The first place you might want to begin is learning everything you can about HTTP protocol and the PHP cURL library.

Counting words on a web page is relatively easy.
<?php // RAY_count_words.php
error_reporting(E_ALL);
echo "<pre>";

// DEMONSTRATE HOW TO COUNT ALL THE WORDS USED ON A WEB PAGE

// USEFUL MAN PAGES:
// http://php.net/manual/en/function.file-get-contents.php
// http://php.net/manual/en/function.preg-replace.php
// http://php.net/manual/en/function.explode.php
// http://php.net/manual/en/array.sorting.php

// ACQUIRE THE DATA
$url = 'http://www.apache.org/';
$htm = file_get_contents($url);

// MUNG THE DATA INTO LOWER-CASE
$htm = strtolower($htm);

// REMOVE CSS AND JAVASCRIPT
$htm = preg_replace("/\<style.*style\>/", NULL, $htm);
$htm = preg_replace("/\<script.*script\>/", NULL, $htm);

// REMOVE THE HTML TAGS
$htm = strip_tags($htm);

// REMOVE EVERYTHING ELSE BUT LETTERS AND BLANKS
$htm = preg_replace('/[^a-z ]/', ' ', $htm);

// CONVERT ANY EXCESS WHITESPACE TO SINGLE BLANKS
$htm = trim(preg_replace('/\s\s+/', ' ', $htm));

// ACTIVATE THIS TO SEE THE "CLEAN" STRING
// echo PHP_EOL . htmlentities($htm);

// MAKE AN ARRAY OF WORDS
$arr = explode(' ', $htm);

// TURN THE ARRAY OF WORDS INTO UNIQUE KEYS, AND ZERO THE COUNTS
$unq = array_flip($arr);
foreach ($unq as $key => $nothing)
{
    $unq[$key] = 0;
}

// COUNT THE WORDS
foreach ($arr as $wrd)
{
    $unq[$wrd]++;
}

// SHOW THE WORK PRODUCTS
echo PHP_EOL . "THERE ARE " . count($unq) . " UNIQUE WORDS AMONG ". count($arr) . " TOTAL WORDS";

echo PHP_EOL . "IN ALPHABETICAL ORDER: ";
ksort($unq);
print_r($unq);

echo PHP_EOL . "IN FREQUENCY ORDER: ";
arsort($unq);
print_r($unq);

Open in new window

Best of luck with the project (it's a big one), ~Ray

Author

Commented:
The different websites are websites of other companies.
Every website is different. The only common think is that one can enter a user name in one text field and the password in the other text field.
We don't own these web sites.

This is about completion and tender checking on  corresponding web sites.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ya, agreed every websites are different, but some have there own api to do so, which we have to integrate.
For our website we have info that what additional data has to be passed for the authentication.
You can use rest ,curl method to pass such parameters.
For example for http://lovetomarry.com beside username/password we have to send additional parameters which you would have to pass through url.

file_get_contents,curl,soap

http://www.electrictoolbox.com/php-file-get-contents-sending-username-password/

Author

Commented:
Thank you,

We have been trying, so far no success. For example a Joomla site has many hidden parameters for example hash...
Most Valuable Expert 2011
Top Expert 2016
Commented:
Yes, it's not an easy task at all and there is very little debugging information available.  You have to write your own debugging algorithms to print out the intermediate data.  When I did this the first time, it took me two full days to get it working correctly, and I already knew HTTP and cURL very thoroughly.  Web tokens and honeypot form fields are not easy things to isolate and overcome.  And if the target script has a CAPTCHA test, you can pretty much forget about it.

A more productive approach might be to contact the owners of the sites and ask if they expose an API.  You may be able to license the rights to use the API to get their data.

Best of luck, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial