Strange problem with get_contents and twitter

I made this function to check Twitter user credentials. It runs on two different web servers.

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

}

?>

It works fine on my web server. On the other hand, it ALWAYS returns 1! Even when the password is intentionally wrong.

What in the world can make one server do one thing and the other do something else?

+3
source share
6 answers

I came up with an alternative solution.

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

}

?>
+1
source

URL- /, -, , . file_get_contents() FALSE, URL.

, , , , auth.

+2

'@' , ( ).

PHP- HTTP, cURL API Twitter, , .

+1

@( ) _get_contents . , . , - php. , allow_url_fopen file_get_contents URL-. (, ini_get() phpinfo().

0

, booleans , , , , .

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

( ), , .

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

Source: https://habr.com/ru/post/1716749/


All Articles