Solved

Get Twitter images by hashtag in php

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

Thanks!
0
Comment
Question by:ivanblue
  • 3
4 Comments
 
LVL 108

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 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This Micro Tutorial will give you a basic overview of Skype through its settings, interface, and features.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now