如何从 url 上传文件?

how to upload file from url?

我已经开发了一个 API 功能来 post 一些东西到 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 \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