gregavola
gregavola

Reputation: 2539

Posting a Photo to Fousquare API V2

I'm trying to post a photo to Foursquare API using the /photos/add method, and I'm having a little difficulites. I either get a 401 (missing file) or 502 (foursquare is down). Here is my code:

$ch = curl_init();

// file image name and it's located in the same folder

$image = "cupcakes.jpg";

// I've tried all of these and no luck   
$s = array("file" => "@".$image, "photo" => "@".$image, "image" => "@".$image);

// I've also tried to send raw data:
//curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($image));

$url = "https://api.foursquare.com/v2/photos/add?oauth_token=TOKENHERE&checknId=4fecf6abe4b0369bc7389903";

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $s);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// image/jpeg type
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: image/jpeg"));

$result = curl_exec($ch);

$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// I get either 502 / 401 (Foursquare is down or File is missing)
echo $result;

Anyone have any idea what I'm doing wrong? The documentation for the endpoint is here: https://developer.foursquare.com/docs/photos/add -- in particular it implies that the photo data should be body of the POST request?

Upvotes: 1

Views: 741

Answers (1)

gregavola
gregavola

Reputation: 2539

I've fixed the problem, turns out the param that you need to send to foursquare is "photo". All the other params must be included in the POST array as well.

Even though the content-type is requested as "image/jpeg", I've just put in "Except:" and it works fine. I'm not 100% sure why that goes through, but it does. Updated code below:

  $s = array("photo" => "@".$image, "checkinId" => "4fecf6abe4b0369bc7389903");

  $url = "https://api.foursquare.com/v2/photos/add?oauth_token=TOKENHERE&v=20120609";
  .....

  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));

Everything else can stay the same. Happy posting!

Upvotes: 1

Related Questions