Solved

Get Twitter images by hashtag in php

Posted on 2014-02-27
4
1,532 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 109

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 109

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 109

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Video by: syed
The goal of the tutorial is to teach the what Skype is and how to use it.
This Micro Tutorial will give you a basic overview of Skype through its settings, interface, and features.

815 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

12 Experts available now in Live!

Get 1:1 Help Now