dongzhila3786 2012-07-02 22:54
浏览 55
已采纳

将照片发布到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?

  • 写回答

1条回答 默认 最新

  • douya5194 2012-07-05 15:01
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向