php 如何使用 API 将图片发布到 Instagram
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18844706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post pictures to instagram using API
提问by Altaf Hussain
I am building a php application which needs to post the user uploaded picture directly to Instagram, but after a quick search i found that there is no such function in the API :( and it feels weird... because they should provide one. I am not sure if there is any other way (except the apps for android and iOS) to upload picture using php. Kindly give me any sort of idea if there is any possibility.
我正在构建一个需要将用户上传的图片直接发布到 Instagram 的 php 应用程序,但是在快速搜索之后我发现 API 中没有这样的功能 :( 感觉很奇怪......因为他们应该提供一个。我我不确定是否有任何其他方式(除了适用于 android 和 iOS 的应用程序)使用 php 上传图片。如果有可能,请给我任何想法。
I also read this ,
我也读过这个,
How do I share a link and photo with Instagram using PHP
Thank you
谢谢
采纳答案by Albzi
If you read the link you shared, the accepted answer is:
如果您阅读了共享的链接,则接受的答案是:
You cannot post pictures to Instagram via the API.
您无法通过 API 将图片发布到 Instagram。
It seems you can emulate instagram on PC though.
不过,您似乎可以在 PC 上模拟 Instagram。
Bluestacksis an emulator which lets you run android apps on your PC/Mac etc.
Bluestacks是一个模拟器,可让您在 PC/Mac 等上运行 android 应用程序。
I'm not sure how well it works though.
我不确定它的效果如何。
回答by Lance
Update:
更新:
Instagram are now banning accounts and removing the images based on this method. Please use with caution.
Instagram 现在正在禁止帐户并删除基于此方法的图像。请谨慎使用。
It seems that everyone who has answered this question with something along the lines of it can't be done
is somewhat correct. Officially, you cannot post a photo to Instagram with their API. However, if you reverse engineer the API, you can.
似乎每个回答这个问题的人it can't be done
都有些正确。正式地,您不能使用他们的 API 将照片发布到 Instagram。但是,如果您对 API 进行逆向工程,则可以。
function SendRequest($url, $post, $post_data, $user_agent, $cookies) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/'.$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
if($cookies) {
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
} else {
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
}
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($http, $response);
}
function GenerateGuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(16384, 20479),
mt_rand(32768, 49151),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535));
}
function GenerateUserAgent() {
$resolutions = array('720x1280', '320x480', '480x800', '1024x768', '1280x720', '768x1024', '480x320');
$versions = array('GT-N7000', 'SM-N9000', 'GT-I9220', 'GT-I9100');
$dpis = array('120', '160', '320', '240');
$ver = $versions[array_rand($versions)];
$dpi = $dpis[array_rand($dpis)];
$res = $resolutions[array_rand($resolutions)];
return 'Instagram 4.'.mt_rand(1,2).'.'.mt_rand(0,2).' Android ('.mt_rand(10,11).'/'.mt_rand(1,3).'.'.mt_rand(3,5).'.'.mt_rand(0,5).'; '.$dpi.'; '.$res.'; samsung; '.$ver.'; '.$ver.'; smdkc210; en_US)';
}
function GenerateSignature($data) {
return hash_hmac('sha256', $data, 'b4a23f5e39b5929e0666ac5de94c89d1618a2916');
}
function GetPostData($filename) {
if(!$filename) {
echo "The image doesn't exist ".$filename;
} else {
$post_data = array('device_timestamp' => time(),
'photo' => '@'.$filename);
return $post_data;
}
}
// Set the username and password of the account that you wish to post a photo to
$username = 'ig_username';
$password = 'ig_password';
// Set the path to the file that you wish to post.
// This must be jpeg format and it must be a perfect square
$filename = 'pictures/test.jpg';
// Set the caption for the photo
$caption = "Test caption";
// Define the user agent
$agent = GenerateUserAgent();
// Define the GuID
$guid = GenerateGuid();
// Set the devide ID
$device_id = "android-".$guid;
/* LOG IN */
// You must be logged in to the account that you wish to post a photo too
// Set all of the parameters in the string, and then sign it with their API key using SHA-256
$data ='{"device_id":"'.$device_id.'","guid":"'.$guid.'","username":"'.$username.'","password":"'.$password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
$sig = GenerateSignature($data);
$data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
$login = SendRequest('accounts/login/', true, $data, $agent, false);
if(strpos($login[1], "Sorry, an error occurred while processing this request.")) {
echo "Request failed, there's a chance that this proxy/ip is blocked";
} else {
if(empty($login[1])) {
echo "Empty response received from the server while trying to login";
} else {
// Decode the array that is returned
$obj = @json_decode($login[1], true);
if(empty($obj)) {
echo "Could not decode the response: ".$body;
} else {
// Post the picture
$data = GetPostData($filename);
$post = SendRequest('media/upload/', true, $data, $agent, true);
if(empty($post[1])) {
echo "Empty response received from the server while trying to post the image";
} else {
// Decode the response
$obj = @json_decode($post[1], true);
if(empty($obj)) {
echo "Could not decode the response";
} else {
$status = $obj['status'];
if($status == 'ok') {
// Remove and line breaks from the caption
$caption = preg_replace("/\r|\n/", "", $caption);
$media_id = $obj['media_id'];
$device_id = "android-".$guid;
$data = '{"device_id":"'.$device_id.'","guid":"'.$guid.'","media_id":"'.$media_id.'","caption":"'.trim($caption).'","device_timestamp":"'.time().'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
$sig = GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
// Now, configure the photo
$conf = SendRequest('media/configure/', true, $new_data, $agent, true);
if(empty($conf[1])) {
echo "Empty response received from the server while trying to configure the image";
} else {
if(strpos($conf[1], "login_required")) {
echo "You are not logged in. There's a chance that the account is banned";
} else {
$obj = @json_decode($conf[1], true);
$status = $obj['status'];
if($status != 'fail') {
echo "Success";
} else {
echo 'Fail';
}
}
}
} else {
echo "Status isn't okay";
}
}
}
}
}
}
Just copy and paste the code above in your text editor, change the few variables accordingly and VOILA! I wrote an articleabout this and I've done it many times. See a demo here.
只需将上面的代码复制并粘贴到您的文本编辑器中,相应地更改几个变量,瞧!我写了一篇关于这个的文章,我已经做了很多次了。在此处查看演示。
回答by Tom Roggero
UPDATEIt is now possible:
更新现在可以:
https://developers.facebook.com/docs/instagram-api/content-publishing
https://developers.facebook.com/docs/instagram-api/content-publishing
The Content Publishing API is a subset of Instagram Graph API endpoints that allow you to publish media objects. Publishing media objects with this API is a two step process — you first create a media object container, then publish the container on your Business Account.
内容发布 API 是 Instagram Graph API 端点的子集,允许您发布媒体对象。使用此 API 发布媒体对象是一个两步过程 - 首先创建一个媒体对象容器,然后在您的企业帐户上发布该容器。
回答by Joshua Dance
Instagram now allows businesses to schedule their posts, using the new Content Publishing Beta endpoints.
Instagram 现在允许企业使用新的内容发布 Beta 端点来安排他们的帖子。
https://developers.facebook.com/blog/post/2018/01/30/instagram-graph-api-updates/
https://developers.facebook.com/blog/post/2018/01/30/instagram-graph-api-updates/
However, this blog post - https://business.instagram.com/blog/instagram-api-features-updates- makes it clear that they are only opening that API to their Facebook Marketing Partners or Instagram Partners.
但是,这篇博文 - https://business.instagram.com/blog/instagram-api-features-updates- 明确表示他们只向 Facebook 营销合作伙伴或 Instagram 合作伙伴开放该 API。
To get started with scheduling posts, please work with one of our Facebook Marketing Partners or Instagram Partners.
要开始安排帖子,请与我们的 Facebook 营销合作伙伴或 Instagram 合作伙伴之一合作。
This link from Facebook - https://developers.facebook.com/docs/instagram-api/content-publishing- lists it as a closed beta.
这个来自 Facebook 的链接 - https://developers.facebook.com/docs/instagram-api/content-publishing- 将其列为封闭测试版。
The Content Publishing API is in closed beta with Facebook Marketing Partners and Instagram Partners only. We are not accepting new applicants at this time.
内容发布 API 仅与 Facebook 营销合作伙伴和 Instagram 合作伙伴一起处于封闭测试阶段。我们目前不接受新申请者。
But this is how you would do it:
但这是你将如何做到的:
You have a photo at...
你有一张照片在...
https://www.example.com/images/bronz-fonz.jpg
You want to publish it with the hashtag "#BronzFonz".
您想使用主题标签“#BronzFonz”发布它。
You could use the /user/media
edge to create the container like this:
您可以使用/user/media
边缘来创建这样的容器:
POST graph.facebook.com
/17841400008460056/media?
image_url=https%3A%2F%2Fwww.example.com%2Fimages%2Fbronz-fonz.jpg&
caption=%23BronzFonz
This would return a container ID (let's say 17889455560051444), which you would then publish using the /user/media_publish edge, like this:
这将返回一个容器 ID(假设为 17889455560051444),然后您将使用 /user/media_publish 边缘发布它,如下所示:
POST graph.facebook.com
/17841405822304914/media_publish
?creation_id=17889455560051444
This example from the docs.
这个例子来自docs。
回答by Dheeraj Thedijje
I tried using IFTTT and many other services but all were doing things or post from Instagram to another platform not to Instagram. I read more to found Instagram does not provide any such API as of now.
我尝试使用 IFTTT 和许多其他服务,但都在做事情或从 Instagram 发布到另一个平台而不是 Instagram。我阅读了更多内容,发现 Instagram 目前没有提供任何此类 API。
Using blue stack is again involving heavy installation and doing things manually only.
使用 blue stack 再次涉及繁重的安装和手动操作。
However, you can use your Google Chrome on the desktop version to make a post on Instagram. It needs a bit tweak.
但是,您可以使用桌面版 Google Chrome 在 Instagram 上发帖。它需要稍微调整一下。
- Open your chrome and browse Instagram.com
- Go to inspect element by right clicking on chrome.
- From top right corener menu drop down on developer tools, select more tool.
- Further select network conditions.
- In the network selection section, see the second section there named user agent.
- Uncheck select automatically, and select chrome for Androidfrom the list of given user agent.
- Refresh your Instagram.com page.
- 打开您的 chrome 并浏览 Instagram.com
- 通过右键单击 chrome 来检查元素。
- 从开发人员工具的右上角核心菜单下拉菜单中,选择更多工具。
- 进一步选择网络条件。
- 在网络选择部分,请参阅名为用户代理的第二部分。
- 取消选中自动选择,然后从给定的用户代理列表中选择chrome for Android。
- 刷新您的 Instagram.com 页面。
You will notice a change in UI and the option to make a post on Instagram. Your life is now easy. Let me know an easier way if you can find any.
您会注意到 UI 的变化以及在 Instagram 上发布帖子的选项。你现在的生活很轻松。如果你能找到任何更简单的方法,请告诉我。
I wrote on https://www.inteligentcomp.com/2018/11/how-to-upload-to-instagram-from-pc-mac.htmlabout it.
我在https://www.intelligentcomp.com/2018/11/how-to-upload-to-instagram-from-pc-mac.html 上写了关于它的文章。
Working Screenshot
工作截图
回答by Amru E.
For users who find this question, you can pass photos to the instagram sharing flow (from your app to the filters screen) on iPhone using iPhone hooks: http://help.instagram.com/355896521173347Other than that, there is currently no way in version 1 of the api.
对于发现此问题的用户,您可以使用 iPhone hooks 将照片传递到 iPhone 上的 Instagram 共享流程(从您的应用程序到过滤器屏幕):http: //help.instagram.com/355896521173347除此之外,目前没有在 api 版本 1 中的方式。
回答by Waheed Sabir
There is no API to post photo to instagram using API , But there is a simple way is that install google extension " User Agent " it will covert your browser to android mobile chrome version . Here is the extension link https://chrome.google.com/webstore/detail/user-agent-switcher/clddifkhlkcojbojppdojfeeikdkgiae?utm_source=chrome-ntp-icon
没有使用 API 将照片发布到 instagram 的 API,但有一种简单的方法是安装谷歌扩展“用户代理”,它将您的浏览器转换为 Android 移动版 chrome 版本。这是扩展链接https://chrome.google.com/webstore/detail/user-agent-switcher/clddifkhlkcojbojppdojfeeikdkgiae?utm_source=chrome-ntp-icon
just click on extension icon and choose chrome for android and open Instagram.com
只需单击扩展程序图标并选择 chrome for android 并打开 Instagram.com