2009-09-03 77 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"; 
}