Solved

Get Twitter images by hashtag in php

Posted on 2014-02-27
4
1,593 Views
Last Modified: 2014-02-28
Hello, how can I get twitter images by hashtag using php?

Thanks!
0
Comment
Question by:ivanblue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39892861
Can you get these manually?  If so, please post a link or similar so we can see how you're able to access the information.
0
 
LVL 1

Author Comment

by:ivanblue
ID: 39892892
Yes, this is how:

https://twitter.com/search?q=%23quehagopormetallica&src=typd&mode=photos

Open in new window


Thanks.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39892974
This feels to me like a bit of a "hack" and there may be a Twitter API that would be a better and more stable choice, but it will at least get the image.  The name of the image file can be located near line 420 in the "view source" of the Twitter web page.  It looks like this:
          <div class="stream-media-grid-items media-grid">
    <span data-status-id="439126301949952000"
  data-url="https://pbs.twimg.com/media/BhgXC4fCAAAXmJ4.jpg:thumb"
  data-resolved-url-thumb="https://pbs.twimg.com/media/BhgXC4fCAAAXmJ4.jpg:thumb"
  data-resolved-url-small="https://pbs.twimg.com/media/BhgXC4fCAAAXmJ4.jpg"
  data-resolved-url-large="https://pbs.twimg.com/media/BhgXC4fCAAAXmJ4.jpg:large"

Open in new window

The script I tested is at this URL:
http://www.iconoun.com/demo/temp_ivanblue.php

<?php // demo/temp_ivanblue.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28376343.html


// URL OF THE RESOURCE:
$url = 'https://twitter.com/search?q=%23quehagopormetallica&src=typd&mode=photos';
$htm = my_curl($url);

// ACTIVATE THIS TO SEE WHAT IS HAPPENING
// echo '<pre>';
// echo htmlentities($htm);

// PARSE THE HTML DOCUMENT TO FIND THE IMAGE URL
$sig = 'data-resolved-url-small="';
$arr = explode($sig, $htm);
$arr = explode('"', $arr[1]);
$url = $arr[0];

// READ THE IMAGE FILE AND SEND IT TO THE BROWSER
$img = file_get_contents($url);
header('Content-Type: image/jpeg');
echo $img;

// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

HTH, ~Ray
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39894942
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Viewers will learn how to use the Hootsuite Dashboard.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question