Lone_Ranger Posted November 26, 2013 Share Posted November 26, 2013 (edited) This code I had worked perfectly before but now I get an error as seen on http://www.sentuamessage.com <?php function fetch_data($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $result = curl_exec($ch); curl_close($ch); return $result; } $count = 1; $access_token = "token here"; $display_size = "standard_resolution"; $result = fetch_data("https://api.instagram.com/v1/users/[user here]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?> in order to display the images I use <?php $count = 2; $result = fetch_data("https://api.instagram.com/v1/users/[user here]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?> obviously if I wanted image 3 to show next to image 2 in a different column I used the code the error code I keep getting is: Warning: Invalid argument supplied for foreach() in /home/content/90/9753290/html/bottom.php on line 68 <?php $count = 3; $result = fetch_data("https://api.instagram.com/v1/users/[user here]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=400 width=400/></a></p>"; ?> though the only code that is referencing to on line 68 is "foreach ($result->data as $photo) {" this code will display the images and work about 30-40% of the time now where as before it would always work. Where have I gone wrong? Edited November 26, 2013 by Lone_Ranger Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/ Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 Are getting one image at a time? if so then you dont need to use foreach. You'd use $img = $result->data->images->{$display_size}; to get the image. Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460162 Share on other sites More sharing options...
Lone_Ranger Posted November 26, 2013 Author Share Posted November 26, 2013 trying to get 7 images in one go but as all my images are on different rows on a table I tried to repeat the information so each image was on it's own row Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460167 Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 (edited) Then output the images in the foreach loop <table> <?php foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; echo "<tr><td>"; // output new table row echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; // output image echo "</tr></td>"; // close table row ?> </table> Edited November 26, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460172 Share on other sites More sharing options...
Lone_Ranger Posted November 26, 2013 Author Share Posted November 26, 2013 I did that originally but the table I have made is very complicated due to different image sizes and what not. Im trying to copy the Instagram top frame they have to show there images (2 small, one top one bottom, 3rd image is the one big one and then next to it 2 rows of 2 small images, 2 small images) you can see the format at how I did it at the bottom of my layout at http://www.sentuamessage.com/ or originally what you have posted in post 2 was what I had originally and worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460174 Share on other sites More sharing options...
Lone_Ranger Posted November 26, 2013 Author Share Posted November 26, 2013 <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td valign=top> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td><?php function fetch_data($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $result = curl_exec($ch); curl_close($ch); return $result; } $count = 1; $access_token = "token goes here"; $display_size = "standard_resolution"; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> <tr> <td><?php $count = 2; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> </table> </td> <td> <?php $count = 3; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=400 width=400/></a></p>"; ?></td> <td valign=top> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td> <?php $count = 4; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> <tr> <td><?php $count = 5; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> </table> </td> <td valign=top> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td><?php $count = 6; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p align=center><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> <tr> <td><?php $count = 7; $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$count}&access_token={$access_token}"); $result = json_decode($result); foreach ($result->data as $photo) { $img = $photo->images->{$display_size}; } echo "<p><a href='{$photo->link}' target=new><img src='{$img->url}' border=0 height=200 width=200/></a></p>"; ?></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> just incase, here is the full code used within the tables in order to have them in the order I want them Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460179 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 26, 2013 Solution Share Posted November 26, 2013 (edited) If the ?count parameter allows you to grab multiple images, for example ?count=7 returns seven images then your code would be <?php function fetch_data($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $result = curl_exec($ch); curl_close($ch); return $result; } $access_token = "token goes here"; $display_size = "standard_resolution"; $number_of_images = 7; // get seven images $result = fetch_data("https://api.instagram.com/v1/users/[user account]/media/recent/?count={$number_of_images}&access_token={$access_token}"); $result = json_decode($result); // place images into an array $images = array(); foreach($result->data as $photo) { // add the image data to the array $images[] = array( 'url' = $photo->images->{$display_size}, 'link' = $photo->link, ); } ?> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td valign="top"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <a href="<?php echo $images[0]['link'] ?>" target="new"><img src="<?php echo $images[0]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> <td align="center"> <a href="<?php echo $images[1]['link'] ?>" target="new"><img src="<?php echo $images[1]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> </table> </td> <td align="center"> <a href="<?php echo $images[2]['link'] ?>" target="new"><img src="<?php echo $images[2]['url']; ?>" border="0" height="400" width="400" /></a> </td> <td valign="top"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <a href="<?php echo $images[3]['link'] ?>" target="new"><img src="<?php echo $images[3]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> <td align="center"> <a href="<?php echo $images[4]['link'] ?>" target="new"><img src="<?php echo $images[4]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> </table> </td> <td valign="top"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <a href="<?php echo $images[5]['link'] ?>" target="new"><img src="<?php echo $images[5]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> <td align="center"> <a href="<?php echo $images[6]['link'] ?>" target="new"><img src="<?php echo $images[6]['url']; ?>" border="0" height="200" width="200" /></a> </td> </tr> <tr> </table> </td> </tr> </table> Your could shorten it further by outputting the table dynamically by looping over the $images array. Rather than hard coding the HTML table. Edited November 26, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460195 Share on other sites More sharing options...
Lone_Ranger Posted November 26, 2013 Author Share Posted November 26, 2013 $images[] = array( 'url' = $photo->images->{$display_size}, 'link' = $photo->link, ); Parse error: syntax error, unexpected '=', expecting ')' in bottom.php on line 62 doesn't like those '=' on the URL LINK statement Im pretty tired and I hear what your saying about the waste of HTML script going on, I'll look into shortening that and making life easier. Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460261 Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 Whoops, my bad. $images[] = array() bit should be // add the image data to the array $images[] = array( 'url' => $photo->images->{$display_size}, 'link' => $photo->link ); Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460264 Share on other sites More sharing options...
Lone_Ranger Posted November 26, 2013 Author Share Posted November 26, 2013 lol yea I did correct that to => earlier on, the error message has gone now. I will look into it as the if you have a look http://www.sentuamessage.com on my bottom.php the INSTAGRAM header image I have shows now but the rest of the document from the header below just dies not wanting to load. Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460265 Share on other sites More sharing options...
Lone_Ranger Posted November 27, 2013 Author Share Posted November 27, 2013 cheers pal corrected the error took a bit of playing around till I restudied my old code vs yours { $images[] = array( 'url' => $photo->images->{$display_size}->url, 'link' => $photo->link, ); } fixes it Quote Link to comment https://forums.phpfreaks.com/topic/284293-instagram-integration-error/#findComment-1460285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.