Skip to content

Commit 46eae7a

Browse files
committed
get a users posts and profile info
1 parent d1cef26 commit 46eae7a

1 file changed

Lines changed: 202 additions & 0 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?php
2+
include 'defines.php';
3+
4+
// instagram endpoint structure
5+
$endpointFormat = 'https://graph.facebook.com/v12.0/{ig-user-id}?fields=business_discovery.username({ig-username}){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count,media{id,caption,like_count,comments_count,timestamp,username,media_product_type,media_type,owner,permalink,media_url,children{media_url}}}&access_token={access-token}';
6+
7+
// instagram endpoint with actuall account id
8+
$endpoint = 'https://graph.facebook.com/v12.0/' . $instagramAccountId;
9+
10+
// user to get
11+
$users = array();
12+
13+
// get user info and posts
14+
$users[] = getUserInfoAndPosts( 'programmer.me', $endpoint, $accessToken );
15+
$users[] = getUserInfoAndPosts( 'justinstolpe', $endpoint, $accessToken );
16+
17+
function getUserInfoAndPosts( $username, $endpoint, $accessToken ) {
18+
// endpoint params
19+
$igParams = array(
20+
'fields' => 'business_discovery.username(' . $username . '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count,media{id,caption,like_count,comments_count,timestamp,username,media_product_type,media_type,owner,permalink,media_url,children{media_url}}}',
21+
'access_token' => $accessToken
22+
);
23+
24+
// add params to endpoint
25+
$endpoint .= '?' . http_build_query( $igParams );
26+
27+
// setup curl
28+
$ch = curl_init();
29+
curl_setopt( $ch, CURLOPT_URL, $endpoint );
30+
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
31+
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
32+
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
33+
34+
// make call and get response
35+
$response = curl_exec( $ch );
36+
37+
// close curl call
38+
curl_close( $ch );
39+
40+
// return nice array
41+
return json_decode( $response, true );
42+
}
43+
?>
44+
<!DOCTYPE html>
45+
<html>
46+
<head>
47+
<title>
48+
Getting a Users Instagram Info and Posts
49+
</title>
50+
<meta charset="utf-8" />
51+
<style>
52+
body {
53+
font-family: 'Helvetica';
54+
}
55+
56+
.pages-list {
57+
display: block;
58+
}
59+
60+
.pages-list-item {
61+
display: inline-block;
62+
vertical-align: top;
63+
margin-top: 10px;
64+
width: 250px;
65+
border: 1px solid #333;
66+
margin-left: 10px;
67+
padding: 10px;
68+
font-size: 9px;
69+
}
70+
71+
.pages-media {
72+
width: 100%;
73+
}
74+
75+
.raw-response {
76+
width: 100%;
77+
height: 600px;
78+
}
79+
80+
.nav-container {
81+
margin-top: 20px;
82+
font-size: 20px;
83+
display: inline-block;
84+
width: 100%;
85+
}
86+
87+
.nav-next {
88+
float: right;
89+
}
90+
91+
.profile-container {
92+
display: inline-block;
93+
width: 100%;
94+
}
95+
96+
.profile-image-container {
97+
float: left;
98+
}
99+
100+
.profile-image-container img {
101+
width: 50px;
102+
border-radius: 50%;
103+
}
104+
105+
.child-media {
106+
width: 70px;
107+
}
108+
109+
textarea {
110+
width: 100%;
111+
height: 500px;
112+
}
113+
</style>
114+
</head>
115+
<body>
116+
<h1>Getting a Users Instagram Info and Posts</h1>
117+
<hr />
118+
<h3>Endpoint: <?php echo $endpointFormat; ?></h3>
119+
<hr />
120+
<br />
121+
<?php foreach ( $users as $userInfo ) : ?>
122+
<div class="profile-container">
123+
<div class="profile-image-container">
124+
<img src="<?php echo $userInfo['business_discovery']['profile_picture_url']; ?>" />
125+
</div>
126+
<div style="float:left;margin-left:20px">
127+
<a target="_blank" href="https://www.instagram.com/<?php echo $userInfo['business_discovery']['username']; ?>">
128+
<h3><?php echo $userInfo['business_discovery']['username']; ?></h3>
129+
</a>
130+
<div style="display:inline-block">
131+
<b><?php echo $userInfo['business_discovery']['media_count']; ?></b> posts
132+
</div>
133+
<div style="display:inline-block;margin-left:20px">
134+
<b><?php echo $userInfo['business_discovery']['followers_count']; ?></b> followers
135+
</div>
136+
<div style="display:inline-block;margin-left:20px">
137+
<b><?php echo $userInfo['business_discovery']['follows_count']; ?></b> following
138+
</div>
139+
</div>
140+
</div>
141+
<div>
142+
<h4><?php echo $userInfo['business_discovery']['name']; ?></h4>
143+
<div>
144+
<?php echo nl2br( $userInfo['business_discovery']['biography'] ); ?>
145+
</div>
146+
<div>
147+
<a target="_blank" href="<?php echo $userInfo['business_discovery']['website']; ?>">
148+
<h3><?php echo $userInfo['business_discovery']['website']; ?></h3>
149+
</a>
150+
</div>
151+
</div>
152+
<ul class="pages-list">
153+
<?php foreach ( $userInfo['business_discovery']['media']['data'] as $media ) : ?>
154+
<li class="pages-list-item">
155+
<?php if ( 'IMAGE' == $media['media_type'] || 'CAROUSEL_ALBUM' == $media['media_type']) : ?>
156+
<img class="pages-media" src="<?php echo $media['media_url']; ?>" />
157+
<?php else : ?>
158+
<video class="pages-media" controls>
159+
<source src="<?php echo $media['media_url']; ?>">
160+
</video>
161+
<?php endif; ?>
162+
<div>
163+
Likes: <?php echo $media['like_count']; ?>
164+
</div>
165+
<div>
166+
Comments: <?php echo $media['comments_count']; ?>
167+
</div>
168+
<h4>
169+
<?php echo nl2br( $media['caption'] ); ?>
170+
</h4>
171+
<div>
172+
Link to Post:
173+
<br />
174+
<a target="_blank" href="<?php echo $media['permalink']; ?>" />
175+
<?php echo $media['permalink']; ?>
176+
</a>
177+
</div>
178+
<br />
179+
Media Type: <?php echo $media['media_type']; ?>
180+
<?php if ( 'CAROUSEL_ALBUM' == $media['media_type'] ) : ?>
181+
<div>
182+
<?php foreach ( $media['children']['data'] as $child ) : ?>
183+
<img class="child-media" src="<?php echo $child['media_url']; ?>" />
184+
<?php endforeach; ?>
185+
</div>
186+
<?php endif; ?>
187+
<br />
188+
<div>
189+
Posted at: <?php echo $media['timestamp']; ?>
190+
</div>
191+
</li>
192+
<?php endforeach; ?>
193+
</ul>
194+
<br />
195+
<h3>Raw Response for <?php echo $userInfo['business_discovery']['username']; ?></h3>
196+
<textarea><?php print_r( $userInfo ); ?></textarea>
197+
<br />
198+
<hr />
199+
<br />
200+
<?php endforeach; ?>
201+
</body>
202+
</html>

0 commit comments

Comments
 (0)