【问题标题】:how to upload file from url?如何从 url 上传文件?
【发布时间】:2019-02-09 11:01:04
【问题描述】:

我开发了一个 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 \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";
    }

感谢阅读,希望有解决这个问题的办法。

【问题讨论】:

  • 用file_get_contents读取文件存储在服务器上,上传,删除?

标签: php codeigniter instagram


【解决方案1】:

如果没有其他方法可以将图片的 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

【讨论】:

    猜你喜欢
    • 2019-03-08
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2012-01-08
    • 2022-07-12
    相关资源
    最近更新 更多