2009-09-03 5 views
3

이 기능을 사용하여 사용자의 트위터 자격 증명을 확인했습니다. 두 개의 다른 웹 서버에서 실행됩니다.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()를 참조 : 아래

코드는 두 경우 모두를 위해 작동합니다. –

+1

문자열은 1이나 0이 아니라 true 또는 false를 반환해야합니다. – Garrett

답변

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()의 반환 값이 무엇인지 궁금합니다. –

+0

@Mike B : 부울 false. – jimyi

+0

@jimyi 그의 원래 질문은 그의 함수가 항상 1을 반환했다는 것을 의미합니다. 즉, file_get_contents()가 false가 아닌 다른 것을 반환하고 있음을 의미합니다. –

1

오류 메시지 (있는 경우)를 보려면 기능에서 '@'기호를 제거하십시오.

일부 PHP 구성에서는 HTTP 프로토콜을 통해 파일을 열 수 없으므로 cURL을 살펴 보거나 공식 Twitter API를 사용하여 인증 기능이 있는지 확인하십시오.

2

사용자 이름/비밀번호의 조합으로 URL을 방문하면 인증 성공 여부에 관계없이 항상 결과를 반환합니다. file_get_contents()는 요청 된 URL을 열지 못하면 FALSE 만 반환합니다.

당신의 함수가 성공적이라면 인증 성공 여부를 결정하기 위해 반환 값을 구문 분석해야 할 것입니다.

+1

어쩌면 브라우저를 통해,하지만 난 그냥 그의 코드를 시도하고'file_get_contents'는 인증이 실패 할 때 FALSE를 리턴합니다 (오류를 억제 할 때 경고와 함께). – jimyi

+0

경고 메시지가 무엇인가요? –

+0

내 문제의 원인이되는 두 번째 서버에 대해 다른 점은 무엇입니까? – mrpatg

0

file_get_contents 앞에 @ 기호 (오류 억제)를 사용하면 오류가 표시되지 않을 수 있습니다. 그것을 제거하고 어떤 오류가 있는지보십시오. 또한 PHP 구성으로 인해 다른 서버에서 다른 동작을 보게 될 수도 있습니다. 특히 allow_url_fopen 설정은 file_get_contents URL에 대한 작업 기능을 변경합니다. 두 서버에서이 설정을 확인하거나 (ini_get()을 사용하거나 phpinfo()의 결과에서 설정을 찾으십시오.)

+0

allow_url_fopen에 문제가있는 경우 file_get_contents는 항상 false를 반환합니다. 여기서는 그렇지 않습니다. – jimyi

+0

사실이 아닙니다. file_get_contents에 문제가있는 경우 file_get_contents는 전혀 반환 할 기회조차 없습니다. 오류가있는 함수에서 빠져 나옵니다. @ 기호는 항상 true를 반환합니다. – Asaph

+0

시도해 보셨습니까? 실제로 이것을 시도하고, 오류가있는 경우 부울 값을 반환하거나 부울 값을 반환하거나 allow_url_fopen을 사용할지 여부를 반환합니다. – jimyi

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

+0

그는 유효한 사용자인지 확인하기 위해 응답을 구문 분석해야합니다. 왜 동일한 응답을 사용하여 모든 사람을 투표합니까? 나에게 어리석은 것처럼 보인다. – Garrett

+0

나는 그들이 좋은 제안 일지 모르지만, 자신의 특별한 문제를 해결하지 않기 때문에 downvoting 해요. – jimyi

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"; 
}