1

我正在尝试使用 /photos/add 方法将照片发布到 Foursquare API,但我遇到了一些困难。我要么得到 401(缺少文件)要么得到 502(foursquare 已关闭)。这是我的代码:

$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;

有人知道我做错了什么吗?端点的文档在这里:https ://developer.foursquare.com/docs/photos/add - 特别是它暗示照片数据应该是 POST 请求的正文?

4

1 回答 1

1

我已经解决了这个问题,原来你需要发送到foursquare的参数是“照片”。所有其他参数也必须包含在 POST 数组中。

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!

于 2012-07-05T15:01:21.093 回答