我已经开发了一个API函数来发布一些东西到Instagram而不必登录,问题是图像必须是本地的(在计算机中,d:\ .. e:\ ..等)我想从网址上传图像例如文件(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 \InstagramAPI\Instagram($debug, $truncatedDebug);
try {
$ig->login($username, $password);
} catch (\Exception $e) {
//echo 'Something went wrong: '.$e->getMessage()."\n";
exit(0);
}
try {
$photo = new \InstagramAPI\Media\Photo\InstagramPhoto($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 \InstagramAPI\Media\Photo\InstagramPhoto(__DIR__.'/img.png');
$ig->timeline->uploadPhoto($photo->getFile(), ['caption' => $captionText]);
unlink(__DIR__.'/img.png'); // Remove file from server