Skip to content

Commit 1fbd697

Browse files
committed
instagram graph api publishing content
1 parent c15ef64 commit 1fbd697

4 files changed

Lines changed: 213 additions & 3 deletions

File tree

instagram_graph_api/obtaining_access_token.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@
4545
echo '<h1>Long Lived Access Token</h1>';
4646
print_r( $accessToken );
4747
} else { // display login url
48-
$permissions = ['public_profile', 'instagram_basic', 'pages_show_list', 'instagram_manage_insights', 'instagram_manage_comments', 'manage_pages'];
48+
$permissions = [
49+
'public_profile',
50+
'instagram_basic',
51+
'pages_show_list',
52+
'instagram_manage_insights',
53+
'instagram_manage_comments',
54+
'manage_pages',
55+
'ads_management',
56+
'business_management',
57+
'instagram_content_publish',
58+
'pages_read_engagement'
59+
];
4960
$loginUrl = $helper->getLoginUrl( FACEBOOK_REDIRECT_URI, $permissions );
5061

5162
echo '<a href="' . $loginUrl . '">
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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>

sendgrid/config.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?php
2+
// your sendgrid api key
23
define( 'SENDGRID_API_KEY', 'YOUR-SENDGRID-API-KEY' );
34

5+
// from email address
46
define( 'FROM_EMAIL', 'YOUR-FROM-EMAIL' );
5-
define( 'FROM_NAME', 'YOUR-FROM-EMAIL-NAME' );
67

8+
// from name
9+
define( 'FROM_NAME', 'YOUR-FROM-NAME' );
10+
11+
// to email address
712
define( 'TO_EMAIL', 'YOUR-TO-EMAIL' );
8-
define( 'TO_NAME', 'YOUR-FROM-EMAIL-NAME' );
13+
14+
// to name of user you are sending the email to
15+
define( 'TO_NAME', 'YOUR-TO-NAME' );

sendgrid/send_first_email.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
<?php
2+
// require our config file
23
require_once 'config.php';
4+
5+
// require composer autoload so we can use sendgrid
36
require 'vendor/autoload.php';
47

8+
// create new sendgrid mail
59
$email = new \SendGrid\Mail\Mail();
10+
11+
// specify the email/name of where the email is coming from
612
$email->setFrom( FROM_EMAIL, FROM_NAME );
13+
14+
// set the email subject line
715
$email->setSubject( "Sending with SendGrid is Fun" );
16+
17+
// specify the email/name we are sending the email to
818
$email->addTo( TO_EMAIL, TO_NAME );
19+
20+
// add our email body content
921
$email->addContent( "text/plain", "and easy to do anywhere, even with PHP" );
1022
$email->addContent(
1123
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
1224
);
25+
26+
// create new sendgrid
1327
$sendgrid = new \SendGrid( SENDGRID_API_KEY );
1428

1529
try {
30+
// try and send the email
1631
$response = $sendgrid->send( $email );
32+
33+
// print out response data
1734
print $response->statusCode() . "\n";
1835
print_r( $response->headers() );
1936
print $response->body() . "\n";
2037
} catch ( Exception $e ) {
38+
// something went wrong so display the error message
2139
echo 'Caught exception: '. $e->getMessage() ."\n";
2240
}

0 commit comments

Comments
 (0)