Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Twitter images by hashtag in php

Posted on 2014-02-27
4
Medium Priority
?
1,646 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
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…
The viewer will learn how to dynamically set the form action using jQuery.

670 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