SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Twitter API

    Friends, I am trying to get the data of Twitter users by ID, via API, but as I'm using WHILE is VERY slow. My script is correct? Can you help me find a solution?

    PHP Code:
    <?php
        
    require_once('config.php');
        
        function 
    user($id){
            
    $jsonurl "https://api.twitter.com/1/users/show.json?id=".$id."&include_entities=true";

            
    //Retorna o conteudo do arquivo em formato de string
            
    $json file_get_contents($jsonurl,0,null,null);

            
    //Decodificando a string e criando o json
            
    $json_output json_decode($json);
            
            return 
    $json_output->screen_name;
        }
        
        function 
    img($id){
            
    $jsonurl "https://api.twitter.com/1/users/show.json?id=".$id."&include_entities=true";

            
    //Retorna o conteudo do arquivo em formato de string
            
    $json file_get_contents($jsonurl,0,null,null);

            
    //Decodificando a string e criando o json
            
    $json_output json_decode($json);
            
            return 
    $json_output->profile_image_url;
        }
            
        
    $sql mysql_query("SELECT * FROM content");
        while ( 
    $filter mysql_fetch_object($sql) )
        {
          echo 
    '<img src="'.img($filter->user).'" width="50" height="50" alt="" /><br />';
          echo 
    user($filter->user). '<br /><br />';
        }

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    You can use users/lookup to get up to 100 user data at once. Also you don't need to lookup each user's image, because it's already returned with user data in field profile_image_url. You can replace normal with bigger, mini or original in that url if you need different size of profile image.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already tried that. The problem is not in the picture, but in programming. = /

    Any suggestions, folks?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    This should help you get started.
    PHP Code:
    <?php
    $users 
    = array();
    //get user ids from database
    $dsn 'mysql:dbname=testdb;host=127.0.0.1';
    $user 'dbuser';
    $password 'dbpass';

    try {
        
    $dbh = new PDO($dsn$user$password);
    } catch (
    PDOException $e) {
        echo 
    'Connection failed: ' $e->getMessage();
    }
    $query "SELECT * FROM users LIMIT 100";

    foreach(
    $dbh->query($query) as $row) {
        
    $users[] = $row['twitter_id'];
    }

    $request  "https://api.twitter.com/1/users/lookup.json?user_id=".implode(','$users);
      
    $content json_decode(file_get_contents($request));
      
    foreach(
    $content as $user) {?>
        <div>
            <img src="<?php echo $user->profile_image_url?>" alt="" />
            <a href="http://twitter.com/<?php echo $user->screen_name?>"><?php echo $user->name?></a>
        </div>
    <?php ?>

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked perfectly, thanks.

    I did not know this PHP PDO. Can you tell me why I can not do this?

    PHP Code:
    $query mysql_query("SELECT * FROM conteudo"); 

    foreach(
    $query as $row) { 
         
    $users[] = $row['usuario']; 


  6. #6
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use ORDER BY with PHP PDO? For me it did not work. = /

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Of course, you can use any valid SQL statement with PDO. It's suggested to switch to PDO or MySQLi, because regular MySQL extension is outdated and will get removed in the future.
    Problem in your snippet is that you need fetch the rows:
    PHP Code:
    $query mysql_query("SELECT * FROM conteudo");  

    while(
    $row mysql_fetch_array($query)) {  
         
    $users[] = $row['usuario'];  

    For easier debugging, enable error_reporting and use mysql_errors() to catch query errors.

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. How do I sort the results? Every time I refreshed the page the order changes, even with the ORDER BY.

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    ORDER BY should be working, if you mean sorting database results.
    Show the code which you are using.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •