Skip to content

Commit 0d339b6

Browse files
committed
instagram graph api get users stories
1 parent b9f9402 commit 0d339b6

4 files changed

Lines changed: 217 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Blog -> https://justinstolpe.com/blog/
88

99
My Website -> https://justinstolpe.com
1010

11+
GitHub -> https://github.com/jstolpe/
12+
1113
==========================================================================
1214

1315
( ( ) ( ) ( (

fortnite_item_shop/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ function getStoreJsonFiles() {
124124
$validFiles[] = array(
125125
'file' => $file,
126126
'date' => $namePieces[0],
127-
'link_json' => 'http://' . $_SERVER['HTTP_HOST'] . '/blog_code_done/fortnite_item_shop/store_json_files/' . $file,
128-
'link_store' => 'http://' . $_SERVER['HTTP_HOST'] . '/blog_code_done/fortnite_item_shop/?date=' . $namePieces[0],
127+
'link_json' => 'http://' . $_SERVER['HTTP_HOST'] . '/blog_code/fortnite_item_shop/store_json_files/' . $file,
128+
'link_store' => 'http://' . $_SERVER['HTTP_HOST'] . '/blog_code/fortnite_item_shop/?date=' . $namePieces[0],
129129
);
130130
}
131131
}

index.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
// get files in the current directory
3+
$files = scandir( getcwd() );
4+
?>
5+
<!DOCTYPE html>
6+
<html>
7+
<head>
8+
<title>
9+
Justin Stolpe's YouTube and Blog Code
10+
</title>
11+
12+
<meta charset="utf-8">
13+
<meta name="title" content="Justin Stolpe's YouTube and Blog Code" />
14+
<meta name="description" content="This site contains the code from my YouTube and Blog tutorials." />
15+
<meta name="keywords" content="Programming, Coding, Developer" />
16+
<meta name="author" content="Justin Stolpe" />
17+
<meta name="robots" content="index, follow" />
18+
</head>
19+
<body>
20+
<h1>
21+
Justin Stolpe's YouTube and Blog Code
22+
</h1>
23+
<h2>
24+
Programming | Coding | Developer
25+
</h2>
26+
<hr />
27+
<h3>
28+
Tutorial Code
29+
</h3>
30+
<ul>
31+
<?php foreach ( $files as $file ) : ?>
32+
<?php if ( '.' != $file && '..' != $file && '.git' != $file && 'README.md' != $file && '.htaccess' != $file ) : ?>
33+
<li>
34+
<a href="<?php echo $file; ?>">
35+
<?php echo $file; ?>
36+
</a>
37+
</li>
38+
<?php endif; ?>
39+
<?php endforeach; ?>
40+
</ul>
41+
<hr />
42+
<h3>
43+
Links
44+
</h3>
45+
<ul>
46+
<li>
47+
<a href="https://youtube.com/justinstolpe" target="_blank">
48+
YouTube
49+
</a>
50+
</li>
51+
<li>
52+
<a href="https://justinstolpe.com/blog/" target="_blank">
53+
Blog
54+
</a>
55+
</li>
56+
<li>
57+
<a href="https://justinstolpe.com" target="_blank">
58+
Justin's Website
59+
</a>
60+
</li>
61+
<li>
62+
<a href="https://github.com/jstolpe/" target="_blank">
63+
GitHub
64+
</a>
65+
</li>
66+
</ul>
67+
</body>
68+
</html>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
// include defines for access to global variables
3+
include '../defines.php';
4+
5+
// syntax for api call endpoint
6+
$endpointSyntax = 'GET https://graph.facebook.com/v11.0/{ig-user-id}/stories';
7+
8+
/**
9+
* Get a users stories
10+
*
11+
* @param string $instagramAccountId
12+
* @param string $accessToken
13+
*
14+
* @return array with the api response
15+
*/
16+
function getUserStories( $instagramAccountId, $accessToken ) {
17+
// actual endpoint with a media
18+
$endpoint = 'https://graph.facebook.com/v11.0/' . $instagramAccountId . '/stories';
19+
20+
$params = array( // parameters for the endpoint
21+
'access_token' => $accessToken
22+
);
23+
24+
// make the api call and get a response
25+
$response = makeApiCall( $endpoint, 'GET', $params );
26+
27+
// return data from the response
28+
return $response['data']['data'];
29+
}
30+
31+
/**
32+
* Get media info
33+
*
34+
* @param array $media
35+
* @param string $accessToken
36+
*
37+
* @return array with the api response
38+
*/
39+
function getMediaInfo( $media, $accessToken ) {
40+
// actual endpoint with a media
41+
$endpoint = 'https://graph.facebook.com/v11.0/' . $media['id'];
42+
43+
$params = array( // parameters for the endpoint
44+
'fields' => 'caption,id,ig_id,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username',
45+
'access_token' => $accessToken
46+
);
47+
48+
// make the api call and get a response
49+
$response = makeApiCall( $endpoint, 'GET', $params );
50+
51+
// return data from the response
52+
return $response['data'];
53+
}
54+
55+
/**
56+
* Make a a curl call to an endpoint with params
57+
*
58+
* @param string $endpoint we are hitting
59+
* @param string $type of request
60+
* @param array $params to send along with the request
61+
*
62+
* @return array with the api response
63+
*/
64+
function makeApiCall( $endpoint, $type, $params ) {
65+
// initialize curl
66+
$ch = curl_init();
67+
68+
// create endpoint with params
69+
$apiEndpoint = $endpoint . '?' . http_build_query( $params );
70+
71+
// set other curl options
72+
curl_setopt( $ch, CURLOPT_URL, $apiEndpoint );
73+
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
74+
75+
// get response
76+
$response = curl_exec( $ch );
77+
78+
// close curl
79+
curl_close( $ch );
80+
81+
return array( // return data
82+
'type' => $type,
83+
'endpoint' => $endpoint,
84+
'params' => $params,
85+
'api_endpoint' => $apiEndpoint,
86+
'data' => json_decode( $response, true )
87+
);
88+
}
89+
90+
// get the users stories
91+
$stories = getUserStories( $instagramAccountId, $accessToken );
92+
93+
foreach ( $stories as &$story ) { // loop over the each story element
94+
// add the story media info to the story
95+
$story['media_info'] = getMediaInfo( $story, $accessToken );
96+
}
97+
98+
unset( $story );
99+
?>
100+
<!DOCTYPE html>
101+
<html>
102+
<head>
103+
<title>
104+
Instgram Graph API | IG User | Stories
105+
</title>
106+
</head>
107+
<body>
108+
<h1>
109+
Instgram Graph API | IG User | Stories
110+
</h1>
111+
<h2>
112+
<!-- display syntax for reference -->
113+
<?php echo $endpointSyntax; ?>
114+
</h2>
115+
<h3>
116+
User Stories Response
117+
</h3>
118+
<div>
119+
<!-- dump out the entire response -->
120+
<pre><?php print_r( $stories ); ?></pre>
121+
</div>
122+
<hr />
123+
<?php foreach ( $stories as $story ) : // loop over each story element ?>
124+
<?php if ( 'VIDEO' == $story['media_info']['media_type'] ) : // story media is a video ?>
125+
<div>
126+
<video controls poster="<?php echo $story['media_info']['thumbnail_url']; ?>" style="max-width:300px">
127+
<source src="<?php echo $story['media_info']['media_url']; ?>" />
128+
</video>
129+
</div>
130+
<?php elseif ( 'IMAGE' == $story['media_info']['media_type'] ) : // story media is an image ?>
131+
<div>
132+
<img src="<?php echo $story['media_info']['media_url']; ?>" style="max-width:300px" />
133+
</div>
134+
<?php endif; ?>
135+
<div>
136+
<b>
137+
<?php echo $story['media_info']['username']; ?>
138+
</b>
139+
</div>
140+
<a href="<?php echo $story['media_info']['permalink']; ?>" target="_blank">
141+
View on Instagram
142+
</a>
143+
<?php endforeach; ?>
144+
</body>
145+
</html>

0 commit comments

Comments
 (0)