我开发了一个API函数,无需登录即可将某些内容发布到Instagram,问题是图像必须在本地(在计算机中,d:\ .. e:\ ..等(我想从url文件上传图像,例如(http ://example.com/images/pict1.jpg(,但只能使用本地文件,这里是代码:
set_time_limit(0);
date_default_timezone_set('UTC');
require __DIR__.'../../../vendor/autoload.php';
/////// CONFIG ///////
$username = 'instagram username';
$password = 'instagram pass';
$debug = false;
$truncatedDebug = false;
//////////////////////
/////// MEDIA ////////
$photoFilename = '';
$captionText = '';
//////////////////////
$ig = new InstagramAPIInstagram($debug, $truncatedDebug);
try {
$ig->login($username, $password);
} catch (Exception $e) {
//echo 'Something went wrong: '.$e->getMessage()."n";
exit(0);
}
try {
$photo = new InstagramAPIMediaPhotoInstagramPhoto($photoFilename);
$ig->timeline->uploadPhoto($photo->getFile(), ['caption' => $captionText]);
} catch (Exception $e) {
//echo 'Something went wrong: '.$e->getMessage()."n";
}
感谢您的阅读,我希望有解决此问题的方法。
如果没有其他方法可以将 url 传递给此 API,则将文件下载到服务器,然后上传。
$content = file_get_contents('http://example.com/img'); // Download file from internet
file_put_contents(__DIR__.'/img.png', $content); // Save it's content to server
// Rest of magic
$photo = new InstagramAPIMediaPhotoInstagramPhoto(__DIR__.'/img.png');
$ig->timeline->uploadPhoto($photo->getFile(), ['caption' => $captionText]);
unlink(__DIR__.'/img.png'); // Remove file from server