2012-07-02 85 views
1

我试图使用/ photos/add方法将照片发布到Foursquare API,并且我遇到了一些困难。我要么得到401(丢失的文件)或502(正方形失效)。这里是我的代码:将照片发布到Fousquare API V2

$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请求的主体?

回答

1

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

尽管content-type被请求为“image/jpeg”,但我只是放入了“Except:”,它工作正常。我不是100%确定为什么会这样,但它确实如此。更新后的代码如下:

$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:")); 

其他一切都可以保持不变。快乐发布!

+0

嗨,谢谢你的回答。这真的帮助了我。我仍然在将照片发布到网页时遇到一些问题。照片从我的应用程序发布[完美](https://irs0.4sqi.net/img/general/original/78848026_uT9PcCW72zndRrSMPG72xznsD01SWu1AumFZTRTxSlI.jpg),我得到了200的回应,但我没有看到我的[页面]上的照片, (https://foursquare.com/testhb6)..任何想法? –

+0

你得到的回应是什么?你可以在这里发布吗? – gregavola

+0

回复发表了[这里](http://pastebin.com/VYQ6Fa7a) –

相关问题