1+ <?php
2+ include 'defines.php ' ;
3+
4+ function makeApiCall ( $ endpoint , $ type , $ params ) {
5+ $ ch = curl_init ();
6+
7+ if ( 'POST ' == $ type ) {
8+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint );
9+ curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ params ) );
10+ curl_setopt ( $ ch , CURLOPT_POST , 1 );
11+ } elseif ( 'GET ' == $ type ) {
12+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint . '? ' . http_build_query ( $ params ) );
13+ }
14+
15+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , false );
16+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
17+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
18+
19+ $ response = curl_exec ( $ ch );
20+ curl_close ( $ ch );
21+
22+ return json_decode ( $ response , true );
23+ }
24+
25+ // endpoint formats
26+ $ imagesEndpointFormat = 'https://graph.facebook.com/v5.0/{ig-user-id}/media?image_url={image-url}&caption={caption}&access_token={access-token} ' ;
27+ $ videoEndpointFormat = 'https://graph.facebook.com/v5.0/{ig-user-id}/media?video_url={video-url}&media_type&caption={caption}&access_token={access-token} ' ;
28+ $ publishMediaEndpointFormat = 'https://graph.facebook.com/v5.0/{ig-user-id}/media_publish?creation_id={creation-id}&access_token={access-token} ' ;
29+ $ userApiLimitEndpointFormat = 'https://graph.facebook.com/v5.0/{ig-user-id}/content_publishing_limit ' ;
30+ $ mediaObejctStatusEndpointFormat = 'https://graph.facebook.com/v5.0/{ig-container-id}?fields=status_code ' ;
31+
32+ /***
33+ * IMAGE
34+ */
35+ $ imageMediaObjectEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media ' ;
36+ $ imageMediaObjectEndpointParams = array ( // POST
37+ 'image_url ' => 'http://justinstolpe.com/sandbox/ig_publish_content_img.png ' ,
38+ 'caption ' => 'This image was posted through the Instagram Graph API with a script I wrote! Go check out the video tutorial on my YouTube channel.
39+ .
40+ youtube.com/justinstolpe
41+ .
42+ #instagram #graphapi #instagramgraphapi #code #coding #programming #php #api #webdeveloper #codinglife #developer #coder #tech #developerlife #webdev #youtube #instgramgraphapi
43+ ' ,
44+ 'access_token ' => $ accessToken
45+ );
46+ $ imageMediaObjectResponseArray = makeApiCall ( $ imageMediaObjectEndpoint , 'POST ' , $ imageMediaObjectEndpointParams );
47+
48+ // set status to in progress
49+ $ imageMediaObjectStatusCode = 'IN_PROGRESS ' ;
50+
51+ while ( $ imageMediaObjectStatusCode != 'FINISHED ' ) { // keep checking media object until it is ready for publishing
52+ $ imageMediaObjectStatusEndpoint = ENDPOINT_BASE . $ imageMediaObjectResponseArray ['id ' ];
53+ $ imageMediaObjectStatusEndpointParams = array ( // endpoint params
54+ 'fields ' => 'status_code ' ,
55+ 'access_token ' => $ accessToken
56+ );
57+ $ imageMediaObjectResponseArray = makeApiCall ( $ imageMediaObjectStatusEndpoint , 'GET ' , $ imageMediaObjectStatusEndpointParams );
58+ $ imageMediaObjectStatusCode = $ imageMediaObjectResponseArray ['status_code ' ];
59+ sleep ( 5 );
60+ }
61+
62+ // publish image
63+ $ imageMediaObjectId = $ imageMediaObjectResponseArray ['id ' ];
64+ $ publishImageEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media_publish ' ;
65+ $ publishEndpointParams = array (
66+ 'creation_id ' => $ imageMediaObjectId ,
67+ 'access_token ' => $ accessToken
68+ );
69+ $ publishImageResponseArray = makeApiCall ( $ publishImageEndpoint , 'POST ' , $ publishEndpointParams );
70+
71+ /***
72+ * VIDEO
73+ */
74+ // create media object for video
75+ $ videoMediaObjectEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media ' ;
76+ $ videoMediaObjectEndpointParams = array ( // POST variables
77+ 'media_type ' => 'VIDEO ' ,
78+ 'video_url ' => 'https://justinstolpe.com/sandbox/ig_publish_content_vid.mp4 ' ,
79+ 'caption ' => 'This video was posted through the Instagram Graph API with a script I wrote! Go check out the video tutorial on my YouTube channel.
80+ .
81+ youtube.com/justinstolpe
82+ .
83+ #instagram #graphapi #instagramgraphapi #code #coding #programming #php #api #webdeveloper #codinglife #developer #coder #tech #developerlife #webdev #youtube #instgramgraphapi ' ,
84+ 'access_token ' => $ accessToken
85+ );
86+ $ videoMediaObjectResponseArray = makeApiCall ( $ videoMediaObjectEndpoint , 'POST ' , $ videoMediaObjectEndpointParams );
87+
88+ // set status to in progress
89+ $ videoMediaObjectStatusCode = 'IN_PROGRESS ' ;
90+
91+ while ( $ videoMediaObjectStatusCode != 'FINISHED ' ) { // keep checking media object until it is ready for publishing
92+ $ videoMediaObjectStatusEndpoint = ENDPOINT_BASE . $ videoMediaObjectResponseArray ['id ' ];
93+ $ videoMediaObjectStatusEndpointParams = array ( // endpoint params
94+ 'fields ' => 'status_code ' ,
95+ 'access_token ' => $ accessToken
96+ );
97+ $ videoMediaObjectResponseArray = makeApiCall ( $ videoMediaObjectStatusEndpoint , 'GET ' , $ videoMediaObjectStatusEndpointParams );
98+ $ videoMediaObjectStatusCode = $ videoMediaObjectResponseArray ['status_code ' ];
99+ sleep ( 5 );
100+ }
101+
102+ // publish video
103+ $ videoMediaObjectId = $ videoMediaObjectResponseArray ['id ' ];
104+ $ publishVideoEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media_publish ' ;
105+ $ publishVideoEndpointParams = array (
106+ 'creation_id ' => $ videoMediaObjectId ,
107+ 'access_token ' => $ accessToken
108+ );
109+ $ publishVideoResponseArray = makeApiCall ( $ publishVideoEndpoint , 'POST ' , $ publishVideoEndpointParams );
110+
111+ /***
112+ * API LIMIT
113+ */
114+ // check user api limit
115+ $ limitEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/content_publishing_limit ' ;
116+ $ limitEndpointParams = array ( // get params
117+ 'fields ' => 'config,quota_usage ' ,
118+ 'access_token ' => $ accessToken
119+ );
120+ $ limitResponseArray = makeApiCall ( $ limitEndpoint , 'GET ' , $ limitEndpointParams );
121+ ?>
122+ <!DOCTYPE html>
123+ <html>
124+ <head>
125+ <title>
126+ Instagram Graph API Content Publishing
127+ </title>
128+ <style>
129+ body {
130+ font-family: 'Helvetica';
131+ }
132+
133+ .raw-response {
134+ width: 100%;
135+ height: 100px;
136+ }
137+ </style>
138+ </head>
139+ <body>
140+ <h1>Instagram Graph API Content Publishing</h1>
141+ <hr />
142+ <h3>Media Object Image Endpoint: <?php echo $ imagesEndpointFormat ; ?> </h3>
143+ <h3>Raw Response</h3>
144+ <textarea class="raw-response">
145+ <?php print_r ( $ imageMediaObjectResponseArray ); ?>
146+ </textarea>
147+ <hr />
148+ <h3>Publish Image Endpoint: <?php echo $ publishMediaEndpointFormat ; ?> </h3>
149+ <h3>Raw Response</h3>
150+ <textarea class="raw-response">
151+ <?php print_r ( $ publishImageResponseArray ) ; ?>
152+ </textarea>
153+ <hr />
154+ <h3>Media Object Video Endpoint: <?php echo $ videoEndpointFormat ; ?> </h3>
155+ <h3>Raw Response</h3>
156+ <textarea class="raw-response">
157+ <?php print_r ( $ videoMediaObjectResponseArray ) ; ?>
158+ </textarea>
159+ <hr />
160+ <h3>Publish Video Endpoint: <?php echo $ publishMediaEndpointFormat ; ?> </h3>
161+ <h3>Raw Response</h3>
162+ <textarea class="raw-response">
163+ <?php print_r ($ publishVideoResponseArray ) ; ?>
164+ </textarea>
165+ <hr />
166+ <h3>User API Limit Endpoint: <?php echo $ userApiLimitEndpointFormat ; ?> </h3>
167+ <h3>Raw Response</h3>
168+ <textarea class="raw-response">
169+ <?php print_r ( $ limitResponseArray ) ; ?>
170+ </textarea>
171+ <hr />
172+ <h3>Media Status Endpoint: <?php echo $ mediaObejctStatusEndpointFormat ; ?> </h3>
173+ </body>
174+ </html>
0 commit comments