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 配列に含める必要があります。

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

他のすべては同じままでかまいません。嬉しい投稿!

于 2012-07-05T15:01:21.093 に答える