Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use cURL's '@' syntax with a remote URL?

Tags:

file

php

curl

I know this to be true:

... you can post a file that is already on the filesystem by prefixing the filepath with "@".

However, I'm trying to POST a file with cURL that's not local. It's stored on some other URL. Let's just say this photo is Google's logo (it isn't). The URL of that is https://www.google.com/images/srpr/logo11w.png. So I would think that you would do something like this:

$file = file("https://www.google.com/images/srpr/logo11w.png");
// some more stuff
// define POST data
$post_data = array('somekey' => 'somevalue', 
                    'image' => '@' . $file);

However, this doesn't seem to work for whatever reason. Also, I tried using 'image' => '@' . file_get_contents($url). Again, that didn't work.

It looks like a way to get around this is to use a temporary file. Is this the only solution to this problem? In either case, how can I solve this problem?

like image 955
hichris123 Avatar asked Mar 29 '14 20:03

hichris123


1 Answers

You can not use any http url for the file path at curl. You have to use local file. So first download the file into a temporary directory.

file_put_contents("/var/tmp/xyz/output.jpg", file_get_contents("https://www.google.com/images/srpr/logo11w.png"));

Then use this temporary file into your curl:

'image' => '@/var/tmp/xyz/output.jpg'
like image 155
Sabuj Hassan Avatar answered Nov 01 '22 07:11

Sabuj Hassan