Php 如何从url上传文件?
我开发了一个API函数,可以在不必登录的情况下向Instagram发布一些内容,问题是图像必须位于计算机的本地,d:\。。e:\。。etc我想从url文件上传图像,例如http://example.com/images/pict1.jpg,但仅适用于本地文件,代码如下:Php 如何从url上传文件?,php,codeigniter,instagram,Php,Codeigniter,Instagram,我开发了一个API函数,可以在不必登录的情况下向Instagram发布一些内容,问题是图像必须位于计算机的本地,d:\。。e:\。。etc我想从url文件上传图像,例如http://example.com/images/pict1.jpg,但仅适用于本地文件,代码如下: set_time_limit(0); date_default_timezone_set('UTC'); require __DIR__.'../../../vendor/autoload.php'; /
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
读取包含文件内容的文件\u获取\u将其存储在服务器上,上载,删除?