Jump to content

instagram integration error


Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by Lone_Ranger
Link to comment
https://forums.phpfreaks.com/topic/284293-instagram-integration-error/
Share on other sites

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 by Ch0cu3r

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.

 

    <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

  • Solution

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 by Ch0cu3r

 

    $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.

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.