Byte Ninja
Byte Ninja

Reputation: 3

how to upload file from url?

I have developed an API function to post something to Instagram without having to log in, the problem is the image must be locally (in the computer, d: \ .. e: \ .. etc) I want to upload images from the url file for example (http : //example.com/images/pict1.jpg) but only work with local file, here the code:

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";
    }

thank for reading, I wish there is solution for this problem.

Upvotes: 0

Views: 342

Answers (1)

Justinas
Justinas

Reputation: 43507

If there is no other way to pass url to image to this API, then download file to server and then upload it.

$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

Upvotes: 1

Related Questions