サンプル/PHP/新商品検索
#!/usr/bin/php
<?php
/**
* モバイルダイエットAPI ver.1 クライアントサンプル 新商品検索
*
* モバイルダイエットAPIを使用して新商品検索を実行するサンプル <br />
* 要 HTTP_request2 ( "pear install HTTP_Request2" でインストール) <br />
*
* @package MobadaiSample
* @author west@imd.co.jp
* @version 1.0
*/
require_once 'HTTP/Request2.php';
define(MYSCHEME, "https"); // スキーマ, http or https
define(MYSERVER, "*** fill it your server ***"); // サーバー名
define(MYACCESSCODE, "*** fill it your code ***"); // アクセスコード
define(APIVERSION, "2"); // APIバージョン
define(SEARCHTYPE, "newfood"); // 検索タイプ
define(RESPONSETYPE, "json"); // レスポンスタイプ
$request = new HTTP_Request2();
$request->setConfig(
array(
'timeout' => 300,
'ssl_verify_host' => false,
'ssl_verify_peer' => false,
)
);
$word = "カレー チキン"; // 検索文字列。文字コードはUTF-8であること。
$url = MYSCHEME.'://'.MYSERVER.'/services/api'.APIVERSION.'/'.
MYACCESSCODE.'/search/'.SEARCHTYPE.'/'.RESPONSETYPE.'/'.
urlencode($word).'/';
$request->setUrl($url);
echo "--- Request Word\n";
echo $word."\n";
echo "--- Request URL\n";
echo $url."\n";
echo "--- Result\n";
try {
// リクエスト送出
$response = $request->send();
}
catch (Exception $e) {
echo "--- Exception\n";
echo $e->getMessage();
echo "\n";
exit(0);
}
if( $response->getStatus() != 200 ){
echo "HTTP Response Status Error ".$response->getStatus()."\n";
exit(0);
}
$json_resp = json_decode($response->getBody());
for($i=0;$i<count($json_resp->foods);$i++){
echo $i.":".
$json_resp->foods[$i]->objectid.",".
$json_resp->foods[$i]->name.",".
$json_resp->foods[$i]->unit.",".
$json_resp->foods[$i]->stuff_e."\n";
}
echo "--- END\n";
?>