- using $ js = file_get_contents getting data from a third-party site - title
- Further, I receive the received title in search
- I get the search result
The problem is that the function receiving $ js = file_get_contents 2 and more in the response uses one and uses one and gives the result.
That is, if I created 5 blocks in the admin panel (it will output 5 blocks with the results on my site) then $ js = file_get_contents will be processed 5 times, because of which several blocks on the site can be the same, since the results of running five $ js = file_get_contents may be partially the same (we get 5 titles, 2 or 3 of which may be identical), hence there will be repetitions on the site.
How to rewrite or fix my function so that $ js = file_get_contents runs once and displays as many blocks on the site as the title was obtained?
The title is searched in the string $ this-> url --- q = ". urlencode ($ itemtitle)."
Help please, it is very necessary but I will not master 100% :(
protected function getcates($nom){ $part = "snippet"; $js=file_get_contents('сайт.ру/script.js'); if(preg_match('#push\((?<json>.*?)\);#',$js,$match)){ $wares=json_decode($match['json'])->wares; foreach ($wares as $item) { $itemtitle = $item->title; $itemtitle = preg_replace('/[^\p{L}0-9 \!]/iu', ' ', $itemtitle); } } $this->url = "https://www.googleapis.com/youtube/v3/search?key={$this->config["key"]}&part=$part&order=date&maxResults=4&q=".urlencode($itemtitle)."&videoEmbeddable=true&videoSyndicated=true&type=video"; $data = $this->http(TRUE); $html = ""; foreach ($data->items as $yt) { $html .= "РЕЗУЛЬТАТ РАБОТЫ ФУНКЦИИ {$yt->id->videoId}{$yt->snippet->title}"; } if(!empty($html)){ $html = 'шапка блока'.$html.'футер блока'; } return $html; } Thank!
protected function getcates($nom){ $part = "snippet"; $js=file_get_contents('сайт.ру//…'); if(preg_match('#push\((?<json>.*?)\);#',$js,$match)){ $wares=json_decode($match['json'])->wares; $titles = []; foreach ($wares as $item) { $itemtitle = $item->title; $itemtitle = preg_replace('/[^\p{L}0-9 \!]/iu', ' ', $itemtitle); $titles[] = $itemtitle; } } //$html = ""; foreach ($titles as $itemtitle) { $this->url = "https://www.googleapis.com/youtube/v3/search?key={$this->config["key"]}&part=$part&order=date&maxResults=4&q=".urlencode($itemtitle)."&videoEmbeddable=true&videoSyndicated=true&type=video"; $data = $this->http(TRUE); $html = ""; foreach ($data->items as $yt) { $html .= "РЕЗУЛЬТАТ РАБОТЫ ФУНКЦИИ {$yt->id->videoId}{$yt->snippet->title}"; } } if(!empty($html)){ $html = 'шапка блока'.$html.'футер блока'; } return $html; }