views:

486

answers:

2

Hey guys, for the page I am doing needs a login authentication using Twitter (using tweetphp API). For test purposes I used this code below to do a successful login:

if (!isset($_SERVER['PHP_AUTH_USER'])){
header('WWW-Authenticate: Basic realm="Enter your Twitter username and password:"');
header('HTTP/1.0 401 Unauthorized');
    echo 'Please enter your Twitter username and password to view your followers.';
   exit();
   }

$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];

The problem now is, I want to integrate it into a form, so far I have the following:

<form action="logincheck.php" method="post" class="niceform"  >
    <fieldset>
        <legend>Twitter Login:</legend>
        <dl>
            <dt><label for="email">Twitter Username:</label></dt>
            <dd><input type="text" name="username" id="username" size="32" maxlength="128" /></dd>
        </dl>
        <dl>
            <dt><label for="password">Password:</label></dt>
            <dd><input type="password" name="password" id="password" size="32" maxlength="32" /></dd>
        </dl>
            </fieldset>

            <fieldset class="action">
        <input type="submit" name="submit" id="submit" value="Submit" />

I am sending it to logincheck.php, this is where I think I get stuck. I am not sure how to compare the form data with Twitter's login data. I was trying a similar if statement as I used in the first code (box that pops up before page loads), but I couldn't wrap my head around it. Thanks again guys!

A: 

Here's a great tutorial:

Authenticating twitter api with php

Quote from tutorial:

<?php

$username = "yourusername";
$password = "yourpassword";
$twitterHost = "http://twitter.com/favorites.xml";
$curl;

$curl = curl_init();

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_URL, $twitterHost);

$result = curl_exec($curl);
curl_close($curl);

header('Content-Type: application/xml; charset=ISO-8859-1');

print $result;

?>
Vexatus
A: 

thank you so much. this worked perfectly well for me (not using twitter but a different application.)

dlove