file_get_contents和twitter的奇怪问题

时间:2009-09-03 03:33:58

标签: php twitter

我使用此功能来验证用户的Twitter凭据。它运行在两个不同的网络服务器上。

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

}

?>

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

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

6 个答案:

答案 0 :(得分:2)

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

在我看来,为了使您的功能成功,您必须解析返回值以确定auth是否成功。

答案 1 :(得分:1)

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

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

答案 2 :(得分: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";
}

}

?>

答案 3 :(得分:0)

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

答案 4 :(得分: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或不解析响应以确定它们是否经过身份验证。看起来像:

<?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>

如果请求被拒绝(访问不当),则会有一个身份验证对话框下拉,这可能会导致您出现问题。

答案 5 :(得分: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";
}
相关问题