Link to home
Start Free TrialLog in
Avatar of askurat1
askurat1Flag for United States of America

asked on

auto login to website and fillout form

I am looking to write a script that can login to a website and fill out a form and submit it.
I don't really care how it's done whether php or js or a combination of them.

I'm just looking for some guidance to get this going.

Thanks,
Tony
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

Auto login is a risk work you have to concern about this.  However I am going to refer you some  example site for your concept.
http://www.bitrepository.com/php-autologin.html
http://php.about.com/od/advancedphp/qt/php_cookie.htm
Sounds you want to login to someone else's site and fill out the form automatically.  You should know that since that is a common way to generate spam in web site forms, that you will be blocked on a lot of sites these days.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of askurat1

ASKER

I am just trying to auto generate and register a device on apple's developer website.
Here is the link: https://daw.apple.com/cgi-bin/WebObjects/DSAuthWeb.woa/wa/login?appIdKey=d4f7d769c2abecc664d0dadfed6a67f943442b5e9c87524d4587a95773750cea&path=%2F%2Faccount%2Fios%2Fdevice%2FdeviceList.action

I'm obviously not going to give my login info but it should take you to the login page to see what needs to be filled out.

I can also get whatever info is needed once logged in to test.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the response.

Do they have an api for something like this?
If I were to use the script you gave me what changes would need to be made?
You would need to change the following

URL
Name of username field if not userame
Name of password field if not password
Fill in your username
Fill in your password

That should do it.
I made the changes but it doesn't seem to work.
That is not useufl - you need to

a) Show us what you did
b) Tell us what did not work
  - what did you observer
  - what errors were there (if any)

We can only help you if you give us information. One line responses that it did not work does not take us forward.
... but it doesn't seem to work.
The last time I checked, that is not an error message, so we cannot diagnose the problem based on this alone.  We would need the SSCCE that shows what you tried and what happened.  Then we could reproduce the failure and test alternatives.

But to the practical aspects of this... Why are you asking EE if Apple has an API?  Why not just ask Apple?  They are the canonical source of the information you're seeking.  Show Apple this thread (you may need to copy and paste) and ask for their help!
My apologies. Here is what I tried:

<?php // RAY_curl_login.php
error_reporting(E_ALL);
echo "<pre>";

// THE REPLACEMENTS (CASE SENSITIVE) ARE THE LOGIN CREDENTIALS FOR THE SITE
$replacements["theAccountName"] = 'username';
$replacements["theAccountPW"] = 'password';

// READ THE PAGE WITH THE LOGIN FORM
$baseurl = 'https://daw.apple.com/cgi-bin/WebObjects/DSAuthWeb.woa/wa/login?appIdKey=d4f7d769c2abecc664d0dadfed6a67f943442b5e9c87524d4587a95773750cea&path=%2F%2Faccount%2Flogin.action';
$ch = curl_init();

// SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_URL, $baseurl);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR,  'cookie.txt');
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);

// CALL THE WEB PAGE
$htm = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);

// IF ERRORS - SEE http://curl.haxx.se/libcurl/c/libcurl-errors.html
if ($htm === FALSE)
{
    echo PHP_EOL . "CURL GET FAIL: $baseurl CURL_ERRNO=$err ";
    var_dump($inf);
    die();
}


// REMOVE THE END-OF-LINE CHARACTERS
$htm = str_replace(PHP_EOL, NULL, $htm);

// ISOLATE THE FORM
$form   = explode("<form",$htm);
$form   = explode("</form>",$form[1]);
$inputs = explode("<input",$form[0]);
$post   = "";

foreach($inputs as $key => $val)
{
    // IDENTIFY THE ACTION SCRIPT
    $action = strpos($val, "action");
    if($action !== false)
    {
        // EXTRACT THE ACTION SCRIPT NAME FROM THE FORM INPUT
        $actstart = strpos($val, "\"", $action+1);
        $actend   = strpos($val, "\"", $actstart+1);
        $posturl  = substr($val, $actstart+1, ($actend-$actstart-1));
        continue;
    }

    // IDENTIFY THE INPUT FIELDS BY NAME AND VALUE PAIRS
    $name = strpos($val, "name");
    if($name !== false)
    {
        // EXTRACT THE NAME FROM THE FORM INPUT
        $namestart = strpos($val, "\"", $name+1);
        $nameend   = strpos($val, "\"", $namestart+1);
        $strname   = substr($val, $namestart+1, ($nameend-$namestart-1));

        // EXTRACT THE VALUE
        $value = strpos($val, "value");
        if($value !== false)
        {
            $valuestart = strpos($val, "\"", $value+1);
            $valueend   = strpos($val, "\"", $valuestart+1);
            $strvalue   = substr($val, $valuestart+1, ($valueend-$valuestart-1));
        }

        // IF NO VALUE TRY TO REPLACE
        else
        {
            foreach ($replacements as $k => $v)
            {
                if ($k == $strname) $strvalue = $v;
            }
        }
        $post .= "&" . $strname . "=" . urlencode($strvalue);
    }
}

// DATA EXTRACTION COMPLETE -- WAIT A RESPECTABLE PERIOD OF TIME
sleep(1);

// DECLOP LEFTMOST AMPERSAND
$post = substr($post,1);

// SET THE LOGIN URL
$posturl = $baseurl . '/' . $posturl;

// NOW POST THE DATA WE HAVE FILLED IN
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// CALL THE WEB PAGE
$xyz = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);

// IF ERRORS - SEE http://curl.haxx.se/libcurl/c/libcurl-errors.html
if ($xyz === FALSE)
{
    echo PHP_EOL . "CURL POST FAIL: $posturl CURL_ERRNO=$err ";
    var_dump($inf);
}

// NOW ON TO THE NEXT PAGE, USING THE GET METHOD
curl_setopt($ch, CURLOPT_URL, 'https://developer.apple.com/account/ios/device/deviceList.action');
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');

$xyz = curl_exec($ch);
$err = curl_errno($ch);
$inf = curl_getinfo($ch);

// IF ERRORS - SEE http://curl.haxx.se/libcurl/c/libcurl-errors.html
if ($xyz === FALSE)
{
    echo PHP_EOL . "CURL 2ND GET FAIL: $posturl CURL_ERRNO=$err ";
    var_dump($inf);
}

// SHOW OFF THE DATA AFTER THE LOGIN
echo ($xyz);

Open in new window


Obviously I put in my actual username and password.

I attached some screen shots of what I am getting when I run this.

It seems to me like it's not entering the username and password but I could be wrong.

i'm not exactly sure what I am looking for.

User generated imageUser generated imageUser generated image
As I wrote above, ...
this script may serve as a starting point for your work
It's not a black box -- it requires meticulously detailed customization -- this effort you're undertaking is going to take a lot of time and effort.  And if it succeeds at all, I expect that Apple will be unhappy and will take steps to "break" the script you have developed.

Two suggestions, and these will be my last on the subject.

1. Contact Apple for help.
2. Hire a professional developer.

Best regards, and best of luck with your project, ~Ray
It looks like the site is redirecting to the actual login. If you are using curl you should be doing a server side connection to the page - which should not show in the browser.