2009-09-03 75 views
3

我做了这个函数来验证用户的twitter凭据。它运行在两个不同的Web服务器上。与file_get_contents和twitter的奇怪问题

<? 
function twitauth($username, $password){ 
if(@file_get_contents("http://".$username.":".$password."@twitter.com//account/verify_credentials.xml")){ 
    return "1";} 
else { 
    return "0";} 

} 

?> 

在我的网络服务器上,它工作正常。另一方面,它总是返回1!即使密码故意错误。

世界上的什么会导致一台服务器做一件事,另一台做其他事?

+1

检查,看看file_get_contents()函数实际上返回。 – 2009-09-03 03:37:27

+1

您应该返回true或false,而不是1或0作为字符串,尽管它们已足够。 – Garrett 2009-09-03 03:56:07

回答

1

我想出了一个替代解决方案。

<? 
function twitauth($username, $password){ 
$xml = @simplexml_load_file("http://".$username.":".$password."@twitter.com/statuses/friends_timeline.xml"); 
$noway = $xml->error; 
$errorcheck = "Could not authenticate you."; 
if($noway == $errorcheck){ 
return "0"; 
} else { 
return "1"; 
} 

} 

?> 
+0

这是很多多余的信息来获取和解析,只是为了找出用户名/密码组合是否有效。我仍然很好奇,file_get_contents()的返回值是无效的密码。 – 2009-09-03 04:34:01

+0

@Mike B:布尔值false。 – jimyi 2009-09-03 04:47:19

+0

@jimyi他原来的问题表明,他的函数总是返回1,这意味着file_get_contents()返回false以外的内容。 – 2009-09-03 05:48:46

1

从函数中删除'@'符号以查看错误消息(如果有的话)。

某些PHP配置不允许通过HTTP协议打开文件,因此请查看cURL,或尝试查找官方Twitter API以查看它们是否具有身份验证功能供您使用。

2

当我使用用户名/密码的任意组合访问该URL时,无论是auth成功还是失败,它都会返回一些内容。 file_get_contents()仅在无法打开请求的url时才返回FALSE。

在我看来,你的函数是成功的,你将不得不解析返回值来确定auth是否成功。

+1

也许通过浏览器,但我只是尝试了他的代码,当认证失败时('''''''''''''''''''''''''''' – jimyi 2009-09-03 03:44:22

+0

什么是警告? – 2009-09-03 03:45:52

+0

那么会导致我的问题的第二台服务器会有什么不同呢? – mrpatg 2009-09-03 03:46:29

0

file_get_contents前面的@符号(错误抑制)可能会抑制错误。尝试删除它,看看你得到什么错误。另外,由于php的配置,你可能会在不同的服务器上看到不同的行为。具体而言,allow_url_fopen设置更改了file_get_contents使用URL的能力。检查这两个服务器上的设置(可能与ini_get()或找到phpinfo()的输出设置。

+0

如果这是allow_url_fopen的问题,file_get_contents将始终返回false - 这不是这里的情况。 – jimyi 2009-09-03 03:46:01

+0

不正确。如果file_get_contents出现问题,file_get_contents甚至没有机会返回任何东西。它会打破功能的错误。 @符号总是会返回true。 – Asaph 2009-09-03 03:53:42

+0

你试过了吗?我实际上已经尝试过这种方法,并且它返回布尔值错误,带或不带抑制,带或不带allow_url_fopen。 – jimyi 2009-09-03 03:56:33

0

这是一个更新的响应,不返回布尔值作为字符串,它是奇怪的检查,如果它的之前的错误信息检查,如果其错误消息。

<?php 
function twitauth($username, $password){ 
$xml = @simplexml_load_file("http://". urlencode($username) .":". urlencode($password) ."@twitter.com/statuses/friends_timeline.xml"); 
return ($xml->error != "Could not authenticate you.") ? true : false; 
} 
?> 

file_get_contents()将只返回页面的响应,它可以是一个身份验证的用户或坏的响应,你需要使用的SimpleXML或什么不解析回应,以确定他们是否被认证OKS,如:

<?xml version="1.0" encoding="UTF-8"?> 
<user> 
    <id>800316</id> 
    <name>Garrett</name> 
    <screen_name>garrettb</screen_name> 
    <location>WHER>!, CA, USA</location> 
    <description>Build websites, wants to be rich, and loves my Mac. You?</description> 
    <profile_image_url>http://a1.twimg.com/profile_images/185221952/pic_normal.png</profile_image_url> 
    <url></url> 
    <protected>false</protected> 
    <followers_count>158</followers_count> 
    <profile_background_color>352726</profile_background_color> 
    <profile_text_color>3E4415</profile_text_color> 
    <profile_link_color>D02B55</profile_link_color> 
    <profile_sidebar_fill_color>99CC33</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>829D5E</profile_sidebar_border_color> 
    <friends_count>139</friends_count> 
    <created_at>Wed Feb 28 06:03:17 +0000 2007</created_at> 
    <favourites_count>18</favourites_count> 
    <utc_offset>-28800</utc_offset> 
    <time_zone>Pacific Time (US &amp; Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1251845223/images/themes/theme5/bg.gif</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <statuses_count>1781</statuses_count> 
    <notifications></notifications> 
    <verified>false</verified> 
    <following></following> 
    <status> 
    <created_at>Wed Sep 02 19:07:59 +0000 2009</created_at> 
    <id>3716655439</id> 
    <text>@lucaspatton09 take a picture, I want to see.</text> 
    <source>&lt;a href=&quot;http://www.atebits.com/&quot; rel=&quot;nofollow&quot;&gt;Tweetie&lt;/a&gt;</source> 
    <truncated>false</truncated> 
    <in_reply_to_status_id>3716512637</in_reply_to_status_id> 
    <in_reply_to_user_id>59230940</in_reply_to_user_id> 
    <favorited>false</favorited> 
    <in_reply_to_screen_name>lucaspatton09</in_reply_to_screen_name> 
    </status> 
</user> 

如果请求被拒绝(坏的访问),它将有一个身份验证对话框的下拉,这可能是导致您的问题。

+0

他不解析响应,他只是检查file_get_contents()是否返回任何东西。 – jimyi 2009-09-03 03:53:27

+0

他需要解析响应以查看它是否是有效的用户,为什么你用相同的响应投票给每个人?对我来说似乎很愚蠢。 – Garrett 2009-09-03 03:55:35

+0

我投降,因为虽然他们可能是很好的建议,但他们并没有解决他的特殊问题。 – jimyi 2009-09-03 03:57:52

0

file_get_contents通常给出警告,并在遇到HTTP错误代码没有返回,但在你的其他服务器的情况下,它可能会返回错误页面的主体(也许可以通过一些配置来设置)。下面

代码应为这两种情况下工作:

if(strpos(
    @file_get_contents("http://".$username.":".$password."@twitter.com//account/verify_credentials.xml"), 
    "Could not authenticate you.") === false) { 
    echo "credentials ok"; 
} else { 
    echo "credentials not ok"; 
}