Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress-file\u get\u contents循环关闭主页一段时间-替代方案?_Php_Wordpress_Server_Out Of Memory_File Get Contents - Fatal编程技术网

Php Wordpress-file\u get\u contents循环关闭主页一段时间-替代方案?

Php Wordpress-file\u get\u contents循环关闭主页一段时间-替代方案?,php,wordpress,server,out-of-memory,file-get-contents,Php,Wordpress,Server,Out Of Memory,File Get Contents,我有以下问题: 在一个函数中,我放入一个至少有700个名称的数组。我拿出一个数组,里面有过去10天发布的所有信息 该函数通过iTunesAPI获得一个json响应,我想用于进一步分析 问题: -在执行函数时,大约需要3分钟才能完成。 -当我执行主页时,其他人无法访问主页: (服务器上的错误:(70007)指定的超时已过期:AH01075:将请求发送到:(轮询)时出错-->内存不足 问题: -如何更有效地编写此函数? -如何在不占用太多内存的情况下编写此函数,我应该使用unset(…) 代码: 函

我有以下问题: 在一个函数中,我放入一个至少有700个名称的数组。我拿出一个数组,里面有过去10天发布的所有信息

该函数通过iTunesAPI获得一个json响应,我想用于进一步分析

问题: -在执行函数时,大约需要3分钟才能完成。 -当我执行主页时,其他人无法访问主页: (服务器上的错误:(70007)指定的超时已过期:AH01075:将请求发送到:(轮询)时出错-->内存不足

问题: -如何更有效地编写此函数? -如何在不占用太多内存的情况下编写此函数,我应该使用unset(…)

代码:

函数getreleases($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=文件获取内容($ituneslink); $itunesstring=json_解码($itunesstring); /*从json解码到数组的结果*/ 如果($itunesstring->resultCount)>0){ foreach($itunesstring->结果为$value){ 如果((日期差异(日期创建('now')、日期创建($value->releaseDate))->格式('%a'))<10){ #回音“
Gefunden:”.$artist; $releases[]=$value; } } }否则{ echo'Nicht gefunden bei iTunes:“.$artist.”; } unset($ituneslink); 未设置($itunesstring); 未结算($iTunestring2); } 返回$releases; }
问题在于,每次执行该函数时,服务器都需要进行700多个API调用、解析数据并处理逻辑

一个可能的解决方案是使用Wordpress的瞬态来“缓存”值(甚至可能是整个输出),这样,它就不必在每个连接上执行那个费力的函数,它只需从瞬态中提取数据。您可以设置过渡期的到期日期,以便让它每X天/小时重新读取一次信息


请看一个使用瞬变的简单示例

但问题还没有解决。在更新内容、从iTunesAPI获取700个项目以及在for循环中运行时,主页内存不足。虽然我的电脑无法访问主页。我只是尝试了一个“超时”或“睡眠”,所以脚本每隔几秒钟就搜索一次内容。但这并没有改变它

我只是改进了:因为内存原因,我把“foreach”改为“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;
    }
如果(!function_存在('get_contents')){
函数get_contents(&$url){
//如果cURL可用,请使用它。。。
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_超时,10);
$cache=curl\u exec($ch);
卷曲关闭($ch);
返回$cache;
}
}  
函数getfromituneslink(&$link,&$name){
$name=str_replace(“'”、“”、$name);
$name=substr($name,0,14);
$result=get_transient(“getlink_itunes{$name}”);
if(false==$result){
$result=获取内容($link);
设置临时(“getlink_itunes_u{$name},$result,12*HOUR_IN_SECONDS);
}
返回$result;
}
函数getreleases(&$artists){
$releases=array();
而(0resultCount)>0){
#对于($i=0;$i<(计数($itunesstring->results))-1;+$i)
而(0results)){
$value=array\u shift($itunesstring->results);
#$value=&$itunesstring[results][$i];
#foreach($itunesstring->结果为$value)
如果((日期差异(日期创建('now')、日期创建($value->releaseDate))->格式('%a'))<6){
$releases[]=array($value->artistName,$value->collectionName,$value->releaseDate,str_replace(“?uo=4”,”,$value->collectionViewUrl));
未结算(价值);
}
}
}否则{
echo'
Nicht gefunden bei iTunes:'.str_替换(“%20”,“,$artist)。”; } unset($ituneslink); 未设置($itunesstring); } 返回$releases; }
我不知道问题出在哪里-(
任何其他可能让函数运行以逐个获取信息的方法

嘿,谢谢。我刚刚实现了一个transient函数。嘿,不。我仍然有问题(请看我的第二篇文章).Code使用while循环而不是foreach可以很好地工作。Transient可以很好地工作,但是在Transient消失后加载700项仍然是一个问题。也许最好编写一个程序,在程序中有一点中断的情况下一个接一个地更新它?我刚刚再次测试了它。5分钟后,我进入超时((70007)指定的超时已过期:AH01075:将请求分派到:(轮询)-->非r时出错
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;
    }