私はハッシュタグに基づいてInstagramから画像を表示するために、以下のスクリプトを使用しています。それは完璧に動作し、Instagramによって使用可能なすべての画像をリストアップします - 最大20画像。PHP:ループ - 最大アイテムを追加する方法
しかし、私は少ない、例えば10または12の画像を表示することができるようにしたいと思います。
foreach
ループがすべてのアイテムをループしないように、最大アイテム値を保持する変数を追加するにはどうすればよいですか?
PHP:あなたは1-12イメージして、使用しないで他のカウンターを表示したい場合は
<?php
// Enter hashtag;
$hashtag = "nofilter";
$url = "https://www.instagram.com/explore/tags/".$hashtag."/";
$instagram_content = file_get_contents($url);
preg_match_all('/window._sharedData = (.*)\;\<\/script\>/', $instagram_content, $matches);
$txt = implode('', $matches[1]);
$json = json_decode($txt);
foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
?>
カウンターのような "何らかの変数"テル? – j08691
はい、次のようになります。 $ noOfImages = 12; - foreachは12個の画像だけをループします。 – Meek