Wordpress - file_get_contents loop turns down homepage for a while - alternative?

时间:2016-10-20 19:08:27

标签: php wordpress server out-of-memory file-get-contents

i have following problem: in a function, i put in an array with at least 700 names. I get out an array with all information about their releases from the last 10 days.

The function gets via iTunes API a json response, which i want to use for further analyzings.

Problem: - while executing function, it takes about 3mins to finish it. - homepage is not reachable for others, while i execute it: (Error on Server: (70007)The timeout specified has expired: AH01075: Error dispatching request to : (polling)) --> Running out of memory?

Questions: - how to code this function more efficient? - how to code this function without using to much memory, shall i use unset(...) ??

Code:

function getreleases($artists){
#   print_r($artists);
    $releases = array();
    foreach( $artists as $artist){
        $artist = str_replace(" ","%20",$artist);
        $ituneslink = "https://itunes.apple.com/search?term=".$artist."&media=music&entity=album&limit=2&country=DE";
        $itunesstring = file_get_contents($ituneslink);
        $itunesstring = json_decode($itunesstring);
        /*Results being decoded from json to an array*/
        if( ($itunesstring -> resultCount)>0 ){
            foreach ( $itunesstring -> results as $value){          
                if( (date_diff(date_create('now'), date_create( ($value -> releaseDate )))->format('%a')) < 10) {
                    #echo '<br>Gefunden: ' . $artist;
                    $releases[] = $value;
                }
            }
        }else{
            echo '<br><span style="color:red">Nicht gefunden bei iTunes: ' . $artist .'</span>';
        }
        unset($ituneslink);
        unset($itunesstring);
        unset($itunesstring2);
    }
    return $releases;
}

2 个答案:

答案 0 :(得分:0)

The problem lies in the fact that every time that function is executed, your server needs to make 700+ API Calls, parse the data, and work your logic on it.

One potential solution is to use Wordpress's transients to 'cache' the value (or perhaps even the whole output), this way, it won't have to execute that strenuous function on every connection, it'll just pull the data from the transient. You can set an expiry date for a transient, so you can have it refetch the information every X days/hours.

Take a look at this article from CSS Tricks that walks you through a simple example using transients.

答案 1 :(得分:0)

但问题并未解决。在更新内容并从iTunes API获取700项并在for循环中运行时,主页内存不足。虽然我的电脑无法访问主页。我只是尝试了#34;超时&#34;或者&#34;睡觉&#34;每隔几秒钟脚本就会搜索一下这些东西。但它并没有改变它。

我刚刚改进了:改变了&#34; foreach&#34; &#34; for&#34; for&#34;因为记忆原因。现在变量没有被复制。还有更多问题: - / ?? 我必须在那里进行for循环。也许正在复制$ itunesstring?

if(!function_exists('get_contents')){
    function get_contents(&$url){
     // if cURL is available, use it...
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                $cache = curl_exec($ch);
                curl_close($ch);
        return $cache;
    }
}  

  function getfromituneslink(&$link,&$name){
        $name = str_replace("'","",$name);
        $name = substr($name, 0, 14);
        $result = get_transient("getlink_itunes_{$name}");
        if(false === $result){
            $result = get_contents($link);      
            set_transient("getlink_itunes_{$name}",$result, 12*HOUR_IN_SECONDS);
        }
        return $result;
    }



    function getreleases(&$artists){
        $releases= array();
        while( 0 < count($artists)){    
            $itunesstring = array();
            $artist = array_shift($artists);
            $artist = str_replace(" ","%20",$artist);
            $ituneslink = "https://itunes.apple.com/search?term=".$artist."&media=music&entity=album&limit=2&country=DE";
            $itunesstring = getfromituneslink($ituneslink,$artist);
            unset($ituneslink);
            $itunesstring = json_decode($itunesstring);

            if( ($itunesstring -> resultCount)>0 ){
                    #for($i=0; $i< (count($itunesstring -> results))-1; ++$i) 
                while( 0 < count(($itunesstring -> results))){  
                    $value = array_shift($itunesstring -> results);
                    #$value =  &$itunesstring[results][$i];
                    #foreach ( $itunesstring -> results as $value)
                    if( (date_diff(date_create('now'), date_create( ($value -> releaseDate )))->format('%a')) < 6) {
                        $releases[] =  array($value->artistName, $value->collectionName, $value->releaseDate, str_replace("?uo=4","",$value -> collectionViewUrl));
                        unset($value);
                    }
                }
            }else{
                echo '<br><span style="color:red">Nicht gefunden bei iTunes: ' . str_replace("%20"," ",$artist) .'</span>';
            }
            unset($ituneslink);
            unset($itunesstring);
        }
        return $releases;
    }

我不知道问题出在哪里。 :-( 任何其他可能让函数运行以逐个获取信息

相关问题