Solved

Get Twitter images by hashtag in php

Posted on 2014-02-27
4
1,473 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Viewers will learn how to use the Hootsuite Dashboard.

772 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

14 Experts available now in Live!

Get 1:1 Help Now