【问题标题】:404 Not Found nginx/1.12.2 - in foreach with File_get_contents404 Not Found nginx/1.12.2 - 在 foreach 中使用 File_get_contents
【发布时间】:2018-03-07 12:39:38
【问题描述】:

我尝试了多种方法,仍然无法解决。我有一个包含许多 url 的数组,我使用 foreach 访问该站点并使用 file_get_contents() 提取数据

404 Not Found nginx/1.12.2 是大约 2 分钟后出现的错误,并且注册了大约 280 条记录。

除了尝试通过插入代码来执行max_execution_timemysql.connect_timeout还改变ini.php的值01200,结果总是一样的,2分钟后显示错误404 Not Found nginx / 1.12.2

在代码中我尝试使用set_time_limit (0);sleep (1),但也没有解决问题

ini_set('mysql.connect_timeout','0');   
ini_set('max_execution_time', '0');   

function create_wordpress_post_with_code() {

    if( isset( $_POST['mysubmitbtn'] ) ) {
        set_time_limit(0);

        $myGetCsv = function ($str) {
            return str_getcsv($str, ';');
        };

        $lines = array_map($myGetCsv, file(''.get_template_directory_uri() . '/list.csv', FILE_IGNORE_NEW_LINES));    

        foreach($lines as list($var1, $var2)){

            set_time_limit(0);  

        $html = file_get_contents( 'https://example.com/'.$var1.'/'.$var2' ); 
        $document = new DOMDocument();              
        @$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
        $domxpath = new DOMXPath($document);

         // more code here

                $post_id = wp_insert_post(
                            array(
                                'comment_status'    =>   'open',
                                'ping_status'       =>   'closed',
                                'post_author'       =>   $author_id,
                                'post_name'         =>   $slug,
                                'post_title'        =>   $title,
                                'post_content'      =>   $content,
                                'post_status'       =>   'publish',
                                'post_type'         =>   'post'
                            )
                        );
         sleep(1);

        }

    }


} 
add_action( 'init', 'create_wordpress_post_with_code' );

我可以做些什么来解决这个错误?或者除了执行时间之外,发生此错误的可能原因是什么?

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

    当你必须使用变量时,你必须在字符串中使用"。否则它的值将不会被解释。 改变

    $html = file_get_contents( 'https://example.com/$var1/$var2' ); 
    

    $html = file_get_contents("https://example.com/$var1/$var2"); 
    

    或者用.连接字符串

    $html = file_get_contents( 'https://example.com/'.$var1.'/'.$var2); 
    

    【讨论】:

    • 谢谢,但这只是我的问题中的一个错字,代码工作正常,唯一的问题是到达2分钟后没有继续注册列表并显示错误
    猜你喜欢
    • 2021-12-27
    • 2022-08-02
    • 1970-01-01
    • 2020-02-04
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2015-02-20
    相关资源
    最近更新 更多