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+ // set other curl options
16+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
17+
18+ $ response = curl_exec ( $ ch );
19+ curl_close ( $ ch );
20+
21+ return json_decode ( $ response , true );
22+ }
23+
24+ // endpoint formats
25+ $ imagesEnpointFormat = 'https://graph.facebook.com/v5.0/{ig-user-id}/media?image_url={image-url}&caption={caption}&access_token={access-token} ' ;
26+
27+ // create media object for image
28+ $ imageMediaObjectEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media ' ;
29+ $ imageMediaObjectEndpointParams = array ( // POST variables
30+ 'image_url ' => 'https://justinstolpe.com/sandbox/ig_pic_post_location_square.jpg ' ,
31+ 'caption ' => 'Going 127.0.0.1 after work!
32+ .
33+ Youtube.com/justinstolpe
34+ .
35+ #developer #coding #code #learntocode #tutorial #codinglife #html #javascript #jquery #css #php #dev #webdev #tutorial #learntocode #tech #developerlife #programmerhumor #redbull #work ' ,
36+ 'location_id ' => $ pageId ,
37+ 'access_token ' => $ accessToken
38+ );
39+ $ imageMediaObjectResponseArray = makeApiCall ( $ imageMediaObjectEndpoint , 'POST ' , $ imageMediaObjectEndpointParams );
40+
41+ // set status to in progress
42+ $ imageMediaObjectStatusCode = 'IN_PROGRESS ' ;
43+
44+ while ( $ imageMediaObjectStatusCode != 'FINISHED ' ) { // keep checking media object until it is ready for publishing
45+ $ imageMediaObjectStatusEndpoint = ENDPOINT_BASE . $ imageMediaObjectResponseArray ['id ' ];
46+ $ imageMediaObjectStatusEndpointParams = array ( // endpoint params
47+ 'fields ' => 'status_code ' ,
48+ 'access_token ' => $ accessToken
49+ );
50+ $ imageMediaObjectResponseArray = makeApiCall ( $ imageMediaObjectStatusEndpoint , 'GET ' , $ imageMediaObjectStatusEndpointParams );
51+ $ imageMediaObjectStatusCode = $ imageMediaObjectResponseArray ['status_code ' ];
52+ sleep ( 5 );
53+ }
54+
55+ // publish media
56+ $ imageMediaObjectId = $ imageMediaObjectResponseArray ['id ' ];
57+ $ publishImageEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/media_publish ' ;
58+ $ publishEndpointParams = array (
59+ 'creation_id ' => $ imageMediaObjectId ,
60+ 'access_token ' => $ accessToken
61+ );
62+ $ publishImageResponseArray = makeApiCall ( $ publishImageEndpoint , 'POST ' , $ publishEndpointParams );
63+ ?>
0 commit comments