我正在构建一个需要将用户上传的图片直接发布到Instagram的php应用程序,但经过快速搜索后我发现API中没有这样的功能:(感觉很奇怪......因为他们应该提供一。我不确定是否有任何其他方式(除了Android和iOS的应用程序)使用php上传图片。如果有可能,请给我任何想法。
我也读过这个,
How do I share a link and photo with Instagram using PHP
谢谢
答案 0 :(得分:90)
更新
Instagram正在禁止帐户并根据此方法删除图像。请谨慎使用。
似乎每个用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";
}
}
}
}
}
}
只需在文本编辑器中复制并粘贴上面的代码,相应地更改几个变量和VOILA!我写了一篇article关于此事,我已多次完成。查看演示here。
答案 1 :(得分:69)
如果您阅读了您分享的链接,则接受的答案为:
您无法通过API将图片发布到Instagram。
看来你可以在PC上模仿Instagram。
Bluestacks是一个模拟器,可让您在PC / Mac等上运行Android应用程序。
我不确定它的效果如何。
答案 2 :(得分:9)
<强>更新强> 现在可以:
https://developers.facebook.com/docs/instagram-api/content-publishing
Content Publishing API是Instagram Graph API端点的一个子集,允许您发布媒体对象。使用此API发布媒体对象分为两步:首先创建媒体对象容器,然后在您的企业帐户上发布容器。
答案 3 :(得分:5)
对于发现此问题的用户,您可以使用iPhone挂钩将照片传递到iPhone上的Instagram共享流程(从您的应用程序到过滤器屏幕):http://help.instagram.com/355896521173347除此之外,目前还没有版本一个api。
答案 4 :(得分:5)
对于正在寻找有关使用 AWS lambda 和 puppeteer (chrome-aws-lambda)发布到Instagram的解决方案的任何人。请注意,该解决方案仅允许您为每个帖子发布1张照片。如果您不使用lambda,只需将chrome-aws-lambda
替换为puppeteer
。
对于首次启动lambda,由于instagram检测到“可疑登录尝试” ,因此正常运行是不正常的。只需使用PC进入instagram页面并批准,一切就可以了。
这是我的代码,随时对其进行优化:
// instagram.js
const chromium = require('chrome-aws-lambda');
const username = process.env.IG_USERNAME;
const password = process.env.IG_PASSWORD;
module.exports.post = async function(fileToUpload, caption){
const browser = await chromium.puppeteer.launch({
args: [...chromium.args, '--window-size=520,700'],
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: false,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/7.5b3349 Mobile/14F89 Safari/603.2.4');
await page.goto('https://www.instagram.com/', {waitUntil: 'networkidle2'});
const [buttonLogIn] = await page.$x("//button[contains(., 'Log In')]");
if (buttonLogIn) {
await buttonLogIn.click();
}
await page.waitFor('input[name="username"]');
await page.type('input[name="username"]', username)
await page.type('input[name="password"]', password)
await page.click('form button[type="submit"]');
await page.waitFor(3000);
const [buttonSaveInfo] = await page.$x("//button[contains(., 'Not Now')]");
if (buttonSaveInfo) {
await buttonSaveInfo.click();
}
await page.waitFor(3000);
const [buttonNotificationNotNow] = await page.$x("//button[contains(., 'Not Now')]");
const [buttonNotificationCancel] = await page.$x("//button[contains(., 'Cancel')]");
if (buttonNotificationNotNow) {
await buttonNotificationNotNow.click();
} else if (buttonNotificationCancel) {
await buttonNotificationCancel.click();
}
await page.waitFor('form[enctype="multipart/form-data"]');
const inputUploadHandle = await page.$('form[enctype="multipart/form-data"] input[type=file]');
await page.waitFor(5000);
const [buttonPopUpNotNow] = await page.$x("//button[contains(., 'Not Now')]");
const [buttonPopUpCancel] = await page.$x("//button[contains(., 'Cancel')]");
if (buttonPopUpNotNow) {
await buttonPopUpNotNow.click();
} else if (buttonPopUpCancel) {
await buttonPopUpCancel.click();
}
await page.click('[data-testid="new-post-button"]')
await inputUploadHandle.uploadFile(fileToUpload);
await page.waitFor(3000);
const [buttonNext] = await page.$x("//button[contains(., 'Next')]");
await buttonNext.click();
await page.waitFor(3000);
await page.type('textarea', caption);
const [buttonShare] = await page.$x("//button[contains(., 'Share')]");
await buttonShare.click();
await page.waitFor(3000);
return true;
};
// handler.js
await instagram.post('/tmp/image.png', '#text');
如果是url,download it to /tmp folder first,则必须是本地文件路径。
答案 5 :(得分:2)
使用Concrete Social API甚至更简单。请注意,您可以发布图像和视频(meda_type可以是“图像”或“视频”)。
curl --request POST \
--url https://concretesocial.io/1.0/publish \
--data '{ "caption":"Wonderful",
"media_type" : "image",
"media_url" : "https://cdn.pixabay.com/photo/2018/10/28/16/11/landscape-3779159_1280.jpg",
"profiles" : ['5c21013a8c93b0050877659d']"}'
完整披露-我已经构建了此API:)
答案 6 :(得分:2)
我尝试使用IFTTT和许多其他服务,但是所有人都在做事或从Instagram发布到另一个平台而不是Instagram。我了解到更多信息,发现Instagram到目前为止尚未提供任何此类API。
使用蓝色堆栈又需要进行繁重的安装,并且只能手动操作。
但是,您可以在桌面版本上使用Google Chrome浏览器在Instagram上发布信息。它需要一些调整。
您会注意到UI的更改以及可以在Instagram上发帖的选项。 您的生活现在很轻松。 如果您可以找到任何一种简单的方法,请告诉我。
我在https://www.inteligentcomp.com/2018/11/how-to-upload-to-instagram-from-pc-mac.html上写道。
答案 7 :(得分:0)
没有使用API将照片发布到instagram的API,但是有一种简单的方法是安装Google扩展“ User Agent”,它将您的浏览器转换为android mobile chrome版本。这是扩展链接https://chrome.google.com/webstore/detail/user-agent-switcher/clddifkhlkcojbojppdojfeeikdkgiae?utm_source=chrome-ntp-icon
只需点击扩展程序图标,然后为Android选择Chrome并打开Instagram.com
答案 8 :(得分:0)
如果具有UI,则具有“ API”。让我们使用以下示例:我想发布在我创建的任何新博客文章中使用的图片。假设是Wordpress。