我正在做的页面需要使用Twitter进行登录验证(使用tweetphp API)。出于测试目的,我使用以下代码成功登录:
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'];
现在的问题是,我想将它整合到一个表单中,到目前为止,我有以下内容:
<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" />
我将它发送到logincheck.php,这是我认为我陷入困境的地方。我不确定如何将表单数据与Twitter的登录数据进行比较。我正在尝试使用与第一个代码(在页面加载之前弹出的框)中使用的类似的if语句,但我无法绕过它。再次感谢你们!
答案 0 :(得分:1)
这是一个很棒的教程:
Authenticating twitter api with php
引自教程:
<?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;
?>