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!

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

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection