Using PHP and cURL to POST a file

Peter Rukavina

I’ve been experimenting with Share on Ovi this week (seems only proper now that we’re all part of the same family now). Turns out to be a very nicely put-together web app in the Flickr genre, but with much better mobile integration, at least on Nokia devices.

Programatically uploading media to Share on Ovi can be done using the Atom Publishing Protocol: the basic idea is that you do an HTTP POST of binary image data, get back a response that contains an endpoint for doing an HTTP PUT with the image meta-data.

While I’m an old hand at using PHP and cURL together, this was the first time I’d needed to do an HTTP POST that wasn’t a standard key-value pairs, but rather simply a binary file. Turns out this is pretty easy, once you figure it out; the keys are:

...
$upload = file_get_contents($filename);
...
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $upload);
...

Once I got this together, and figured out the secret WSSE voodoo required to authenticate, everything fell into place: here’s the first image I uploaded with my cool new PHP class.

Comments

Submitted by Peter Rukavina on

Permalink

Follow-up: I’ve been experimenting with a script to migrate photos from Flickr to Share on Ovi and it appears to be working (see my Share on Ovi page for some early results). I’ve got the title, description and tags migrating, along with the original image and all its EXIF data.

Submitted by oasisfleeting on

Permalink

Finally, the extra little help I needed.
In this example he is not url-ifying the post data and posting a query string along with the field array. I WAS url-ifying the post data for a query string and it was causing problems.

So do like he did and just pass the $fields array into the setopt

$contents = file_get_contents($_FILES[‘file’][‘tmp_name’]);

$fields = array(
‘filetype’=>’jpg’,
‘fileid’=>/*date(‘YmdGisu’) .*/ $_FILES[“file”][“name”],
‘content’=>$contents
);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible;)”);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

//execute post
$output = curl_exec($ch);

Add new comment

Plain text

  • Allowed HTML tags: <b> <i> <em> <strong> <blockquote> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

About This Blog

Photo of Peter RukavinaI am . I am a writer, letterpress printer, and a curious person.

To learn more about me, read my /nowlook at my bio, listen to audio I’ve posted, read presentations and speeches I’ve written, or get in touch (peter@rukavina.net is the quickest way). 

You can subscribe to an RSS feed of posts, an RSS feed of comments, or a podcast RSS feed that just contains audio posts. You can also receive a daily digests of posts by email.

Search