Note that there are some explanatory texts on larger screens.

plurals
  1. POInstagram API storing images from a tag in mysql
    primarykey
    data
    text
    <p>I am trying to store all images on instagram from a particular tag in my database. My code calls the API and loads the first round of images </p> <pre><code>$json = file_get_contents('https://api.instagram.com/v1/tags/hahanotfunny/media/recent?client_id='. $client_id .'&amp;max_tag_id='. $max_tag_id) ; </code></pre> <p>Once it goes through the first page of the response, it checks to see if there is another page by grabbing the link in the pagination part of the response. It also checks if there is a value for "max_tag_id" and updates my "max" value in my database. The reason I have a max value is so that when I get a response from "real-time" saying there is new images, I start downloading them from the last max time. However, there is a problem with my code. If we are on the last page of the response (no more pagination link) then there is no "max_tag_id" variable, so there is no update to the database. So, next time my scraper runs it starts with the last known "max_tag_id" and this resulting in duplicate images on the last page being recorded in my database. </p> <p>So, my question is, when I get another "real-time" alert saying new images are available for a specific tag, how do I find them starting with the last record stored in my database? </p> <pre><code>$dbConnection = new PDO('mysql:dbname=XXXXXXX;host=127.0.0.1;charset=utf8', 'XXXXXXXXX', 'XXXXX'); $dbConnection-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbConnection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); function getMax() { global $dbConnection; $tag = 'hahanotfunny'; $selectTotals = $dbConnection-&gt;prepare("SELECT * FROM instagram_time WHERE tag = :tag LIMIT 1"); $selectTotals-&gt;execute(array(':tag' =&gt; $tag)); foreach ($selectTotals as $time) { $max = $time['max']; } return $max; } function updateMax($data) { global $dbConnection; $tag = 'hahanotfunny'; $selectTotals = $dbConnection-&gt;prepare("UPDATE instagram_time SET max = :maxid WHERE tag = :tag LIMIT 1"); $selectTotals-&gt;execute(array(':maxid' =&gt; $data, ':tag' =&gt; $tag)); } function fetchData() { global $dbConnection, $client_id; $max_tag_id = getMax(); $json = file_get_contents('https://api.instagram.com/v1/tags/hahanotfunny/media/recent?client_id='. $client_id .'&amp;max_tag_id='. $max_tag_id) ; $data = json_decode($json); $next_max = $data-&gt;pagination-&gt;next_max_tag_id; foreach ($data-&gt;data as $insta) { echo '&lt;br/&gt;&lt;img src="'.$insta-&gt;images-&gt;low_resolution-&gt;url.'"/&gt;'; } foreach ($data as $object) { if ( is_array( $object ) ) { foreach ( $object as $media ) { $url = $media-&gt;images-&gt;standard_resolution-&gt;url; $m_id = $media-&gt;id; $c_time = $media-&gt;created_time; $user = $media-&gt;user-&gt;username; $filter = $media-&gt;filter; $comments = $media-&gt;comments-&gt;count; $caption = $media-&gt;caption-&gt;text; $link = $media-&gt;link; $low_res=$media-&gt;images-&gt;low_resolution-&gt;url; $thumb=$media-&gt;images-&gt;thumbnail-&gt;url; $lat = $media-&gt;location-&gt;latitude; $long = $media-&gt;location-&gt;longitude; $loc_id = $media-&gt;location-&gt;id; $data = array( 'media_id' =&gt; $m_id, 'min_id' =&gt; $next_min_id, 'url' =&gt; $url, 'c_time' =&gt; $c_time, 'user' =&gt; $user, 'filter' =&gt; $filter, 'comment_count' =&gt; $comments, 'caption' =&gt; $caption, 'link' =&gt; $link, 'low_res' =&gt; $low_res, 'thumb' =&gt; $thumb, 'lat' =&gt; $lat, 'long' =&gt; $long, 'loc_id' =&gt; $loc_id, ); $selectTotals = $dbConnection-&gt;prepare("INSERT INTO instagram_mg (media_id, min_id, url, c_time, user, filter, comment_count, caption, link, low_res_link, thumb, latitude, longitude, loc_id) VALUES (:mediaid, :minid, :url, :ctime, :user, :filter, :commentcount, :caption, :link, :lowreslink, :thumb, :latitude, :longitude, :locid)"); $selectTotals-&gt;execute(array(':mediaid' =&gt; $data['media_id'], ':minid' =&gt; $data['min_id'], ':url' =&gt; $data['url'], ':ctime' =&gt; $data['c_time'], ':user' =&gt; $data['user'], ':filter' =&gt; $data['filter'], ':commentcount' =&gt; $data['comment_count'], ':caption' =&gt; $data['caption'], ':link' =&gt; $data['link'], ':lowreslink' =&gt; $data['low_res'], ':thumb' =&gt; $data['thumb'], ':latitude' =&gt; $data['lat'], ':longitude' =&gt; $data['long'], ':locid' =&gt; $data['loc_id'])); } } } if (isset($next_max)) { echo $next_max . "&lt;/br&gt;"; updateMax($next_max); fetchData(); } else { //$current_time = time(); //updateMax($current_time); // i tried making the current time the "max_tag_id" but it wouldnt work. } } //fetchData() fetchData(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload