如何使用API​​将图片发布到Instagram

Alt*_*ain 97 php instagram

我正在构建一个需要将用户上传的图片直接发布到Instagram上的php应用程序,但经过快速搜索后我发现API中没有这样的功能:(感觉很奇怪...因为它们应该提供一个.我我不确定是否有任何其他方式(除了Android和iOS的应用程序)使用php上传图片.如果有任何可能性,请给我任何想法.

我也读过这个,

如何使用PHP与Instagram共享链接和照片

谢谢

Lan*_*nce 94

更新:

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";
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

只需在文本编辑器中复制并粘贴上面的代码,相应地更改几个变量和VOILA!我写了一篇关于这个的文章,我已经多次完成了.在这里看一个演示.

  • 代码工作正常,但guid和deviceid每次都会更改,并且在一次成功发布后,我的帐户就被禁止了.照片也被删除了. (6认同)
  • 如果你得到`status isnt okay`,请确保CURL有权制作cookies.txt文件.`chmod 777/directory`会这样做(小心).我从您的脚本中收到了成功消息,但帖子未在Instagram上显示.这仍然有用吗? (2认同)

Alb*_*bzi 76

如果您阅读了您分享的链接,则接受的答案是:

您无法通过API将图片发布到Instagram.

看来你可以在PC上模仿Instagram.

Bluestacks是一个模拟器,可以让你在你的PC/Mac等上运行Android应用程序.

我不确定它的效果如何.

  • 好吧,如果没有办法,那么我不认为有"另一种"方式. (53认同)
  • 是的,但我问是否还有其他方法? (7认同)
  • 不幸的是,@ usama尚未正式发布,但我听说有传言说,如果您转到他们的网站并将其缩放为移动视图,则可以。我自己没有尝试过 (2认同)
  • @Albzi 如果你使用谷歌浏览器;到 Instagram 网站并右键单击并使用“检查”它会调整大小并将标题更改为移动浏览器签名允许。您可能需要在“检查”中刷新 Instagram 页面一次,但它允许您将该网站用作移动设备,并且可以发布照片等等。然而; 这对 API 问题没有帮助。我希望能够将 PHP 中的照片发布到 Instagram,以显示我们团队的最终得分。 (2认同)

Tom*_*ero 16

更新 现在可以:

https://developers.facebook.com/docs/instagram-api/content-publishing

Content Publishing API是Instagram Graph API端点的子集,允许您发布媒体对象.使用此API发布媒体对象分为两个步骤 - 首先创建媒体对象容器,然后在您的企业帐户上发布容器.

  • 值得注意的是"内容发布API仅与Facebook营销合作伙伴和Instagram合作伙伴处于封闭测试阶段.我们目前不接受新的申请人." (14认同)
  • 说找不到页面! (6认同)

Jos*_*nce 7

Instagram现在允许企业使用新的Content Publishing Beta端点安排其发布时间。

https://developers.facebook.com/blog/post/2018/01/30/instagram-graph-api-updates/

但是,此博客文章-https ://business.instagram.com/blog/instagram-api-features-updates-清楚地表明,他们只是向Facebook营销合作伙伴或Instagram合作伙伴开放该API。

要开始安排帖子,请与我们的Facebook营销合作伙伴或Instagram合作伙伴之一合作。

来自Facebook的此链接-https: //developers.facebook.com/docs/instagram-api/content-publishing-将其列为封闭测试版。

Content Publishing API仅在Facebook Marketing Partners和Instagram Partners的封闭测试版中。我们目前不接受新申请人。

但这是您要执行的操作:

你有一张照片在...

https://www.example.com/images/bronz-fonz.jpg
Run Code Online (Sandbox Code Playgroud)

您想使用标签“ #BronzFonz”发布它。

您可以使用/user/mediaedge这样创建容器:

POST graph.facebook.com 
  /17841400008460056/media?
    image_url=https%3A%2F%2Fwww.example.com%2Fimages%2Fbronz-fonz.jpg&
    caption=%23BronzFonz
Run Code Online (Sandbox Code Playgroud)

这将返回一个容器ID(假设为17889455560051444),然后您可以使用/ user / media_publish边缘将其发布,如下所示:

POST graph.facebook.com
  /17841405822304914/media_publish
    ?creation_id=17889455560051444
Run Code Online (Sandbox Code Playgroud)

这个例子来自docs


Dhe*_*jje 6

我尝试使用IFTTT和许多其他服务,但都是在做事,或者是从Instagram发布到另一个平台而不是Instagram。我阅读了更多内容,发现Instagram到目前为止尚未提供任何此类API。

使用蓝色堆栈再次涉及繁重的安装和仅手动操作。

但是,您可以在桌面版本上使用Google Chrome浏览器在Instagram上发布信息。它需要一点调整。

  1. 打开您的镶边并浏览Instagram.com
  2. 通过右键单击chrome来检查元素。
  3. 在开发人员工具的右上角Corener菜单下拉菜单中,选择更多工具。
  4. 进一步选择网络条件。
  5. 在“网络选择”部分,请参阅第二部分“用户代理”。
  6. 取消选中自动选择,然后从给定的用户代理列表中选择适用于Android的Chrome
  7. 刷新您的Instagram.com页面。

您会注意到UI的更改以及在Instagram上发帖的选项。您的生活现在很轻松。如果您可以找到任何一种简单的方法,请告诉我。

在此处输入图片说明

我在https://www.inteligentcomp.com/2018/11/how-to-upload-to-instagram-from-pc-mac.html上写道。

  • 我刚刚用同样的方法发布了。它工作得非常好。请参阅更新中的屏幕截图。 (2认同)

Amr*_* E. 5

对于发现此问题的用户,您可以使用iPhone挂钩将照片传递到iPhone上的Instagram共享流程(从您的应用程序到过滤器屏幕):http://help.instagram.com/355896521173347除此之外,目前还没有在api的第1版中的方式.

  • 似乎大多数未经授权的客户端通过解密和监控从应用程序到服务器的 SSL 流量来对 API 进行逆向工程。至少 Snapchat 就是这种情况。这里可能是一样的。 (2认同)

小智 5

对于正在寻找有关使用AWS lambdapuppeteer ( chrome-aws-lambda )发布到 Instagram 的解决方案的任何人。请注意,此解决方案仅允许您为每个帖子发布 1 张照片。如果您不使用 lambda,只需替换chrome-aws-lambdapuppeteer.

首次启动 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;
};
Run Code Online (Sandbox Code Playgroud)
// handler.js

await instagram.post('/tmp/image.png', '#text');
Run Code Online (Sandbox Code Playgroud)

必须是本地文件路径,如果是url,先下载到/tmp文件夹