$consumer_key , 'image_size' => '600' , 'rpp' => '20' , 'geo' => '35.681382,139.766084,500km' , ) ; // GETメソッドで指定がある場合 foreach( array( 'only' , 'term' , 'geo' , 'rpp' , 'license_type' , 'sort' ) as $val ) { if( isset( $_GET[ $val ] ) && $_GET[ $val ] != '' ) { $params[ $val ] = $_GET[ $val ] ; } } // リクエストURL $request_url = 'https://api.500px.com/v1/photos/search' . '?' . http_build_query( $params ) ; // リクエストURL // アイテムデータをJSON形式で取得する (CURLを使用) $curl = curl_init() ; // オプションのセット curl_setopt( $curl , CURLOPT_URL , $request_url ) ; curl_setopt( $curl , CURLOPT_HEADER, 1 ) ; curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ; // 証明書の検証を行わない curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ; // curl_execの結果を文字列で返す curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ; // タイムアウトの秒数 // 実行 $res1 = curl_exec( $curl ) ; $res2 = curl_getinfo( $curl ) ; // 終了 curl_close( $curl ) ; // 取得したデータ $json = substr( $res1, $res2['header_size'] ) ; // 取得したデータ(JSONなど) $header = substr( $res1, 0, $res2['header_size'] ) ; // レスポンスヘッダー (検証に利用したい場合にどうぞ) // HTML用 $html = '' ; // JSONデータをオブジェクト形式に変換する $obj = json_decode( $json ) ; // 条件指定用フォーム $html .= '

条件を指定する

' ; $html .= '

条件を指定して、写真を検索してみて下さい。

' ; $html .= '
' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '

' ; $html .= '
' ; // HTMLを作成 $html .= '

実行結果

' ; // エラー判定 if( !$obj || !isset($obj->photos) ) { $html .= '

データを取得できませんでした…。設定を再確認して下さい。

' ; } else { foreach( $obj->photos as $item ) { // 成人コンテンツをスキップ if( isset( $item->nsfw ) && $item->nsfw ) { continue ; } // 各データ $id = $item->id ; // メディアID $name = ( isset($item->name) ) ? $item->name : '' ; // 写真のタイトル $description = ( isset($item->description) ) ? $item->description : '' ; // 写真の説明 $image_url = $item->image_url ; // 写真のURL $created_at = $item->created_at ; // 投稿日時 // ユーザー情報 $user_id = $item->user_id ; // 投稿者ID $user_username = $item->user->username ; // ユーザーネーム $user_avatars = ( isset($item->user->avatars->default->https) ) ? $item->user->avatars->default->https : '' ; // アバター画像 // 場所情報 $country = ( isset($item->location_details->country[0]) ) ? $item->location_details->country[0] : '' ; // 国名 $latitude = ( isset($item->latitude) ) ? $item->latitude : '' ; // 緯度 $longitude = ( isset($item->longitude) ) ? $item->longitude : '' ; // 緯度 // 日付の整形 $created_at = date( 'Y/m/d H:i' , strtotime( $created_at ) ) ; // ブラウザに出力 $html .= '
' ; $html .= '
投稿者のID
' ; $html .= '
' . $user_id . '
' ; $html .= '
投稿者のユーザーネーム
' ; $html .= '
' . $user_username . '
' ; // アイコン画像がある場合 if( $user_avatars ) { $html .= '
投稿者のアイコン
' ; $html .= '
' ; } $html .= '
写真
' ; $html .= '
' ; // 写真のタイトル if( $name ) { $html .= '
写真のタイトル
' ; $html .= '
' . $name . '
' ; } // 写真の説明 if( $description ) { $html .= '
写真の説明
' ; $html .= '
' . $description . '
' ; } // 場所名がある場合 if( $country ) { $html .= '
国名
' ; $html .= '
' . $country . '
' ; } // 緯度、経度がある場合 if( $latitude && $longitude ) { $html .= '
地図
' ; $html .= '
Google Mapsで位置を確認する
' ; } $html .= '
投稿日時
' ; $html .= '
' . $created_at . '
' ; $html .= '
' ; } } // 取得したデータ $html .= '

取得したデータ

' ; $html .= '

下記のデータを取得できました。

' ; $html .= '

JSON

' ; $html .= '

' ; $html .= '

レスポンスヘッダー

' ; $html .= '

' ; ?> 500px APIで写真検索をするサンプルデモ

配布元: Syncer