延长php执行时间

时间:2018-03-19 22:11:27

标签: php curl

尝试通过这些方法延长执行时间,但脚本会过早地结束。它基本上是通过mysql数据库循环并对每一行做一些事情。它应该持续5到10分钟,但在同一地点一直停留。我尝试过以下方法:

 set_time_limit(0);
 curl_setopt($ch, CURLOPT_TIMEOUT, 2000000);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 200000);

他们都没有工作。请帮忙!

    <?php
ini_set('max_execution_time', 500);
$user="5aabf73bdd2c7";
//$user = $_POST['user'];
$servername = "localhost";
$username = "placeprint_1";
$password = "JS313833";
$dbname = "placeprint_1";
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 2000000);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 200000);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
$data = curl_exec($ch);
curl_close ( $ch );
return $data;
}

$sql = "SELECT link, id, cookie FROM rawlinks WHERE cookie='$user' ORDER by ID desc ";
$result = $con->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    $getimagefrom=$row["link"];
    $id=$row["id"];
        $cookie=$row["cookie"];
echo $getimagefrom;
echo "<br><br>ID:".$id."<br><br>";
$htmlaa = file_get_contents_curl($getimagefrom);
$docaa = new DOMDocument();
@$docaa->loadHTML($htmlaa);
$nodesaa = $docaa->getElementsByTagName('title');
$nodesxaa = $docaa->getElementsByTagName('img');
//get and display what you need:
$titlev = $nodesaa->item(0)->nodeValue;
$metasv = $docaa->getElementsByTagName('meta');
$asv = $docaa->getElementsByTagName('img');
for ($iv = 0; $iv < $metasv->length; $iv++)
{
$metav = $metasv->item($iv);
if($metav->getAttribute('name') == 'description')
    $descriptionv = $metav->getAttribute('content');
if($metav->getAttribute('name') == 'keywords')
    $keywordsv = $metav->getAttribute('content');
if($metav->getAttribute('property') == 'og:image');
    $languagev = $metav->getAttribute('content');
}

for ($ivv = 0; $ivv < $asv->length; $ivv++)
{

  $av = $asv->item($ivv);  
echo $av->getAttribute('src');
echo "<br>";
$cvarv = $av->getAttribute('src');
echo " <img src='$cvarv' >";

$servername = "localhost";
$username = "placeprint_1";
$password = "JS313833";
$dbname = "placeprint_1";

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

$getimagefrom = rtrim($getimagefrom, '/');

$cvarv = urlencode($cvarv);
$titlev = preg_replace('/[^\p{L}\p{N}\s]/u', '', $titlev);
$descriptionv = preg_replace('/[^\p{L}\p{N}\s]/u', '', $descriptionv);

}
    }
} 
$con->close();
echo "FINISHED!!!!";
?> 

3 个答案:

答案 0 :(得分:0)

从php.ini尝试增加max_execution_time并重新启动apache服务器。

要找到php.ini的位置,您可以创建一个phpinfo.php文件并插入以下内容:

<?php phpinfo();

此处提供更多信息:https://mediatemple.net/community/products/dv/204643880/how-can-i-create-a-phpinfo.php-page

当您访问浏览器上的phpinfo.php时,您可以获取php.ini配置文件位置的位置。

从代码中我可以建议,不要为每个循环执行 INSERT mysql请求,而应该在循环中准备插入值,然后在循环后只执行一次实际的mysql请求结束。

另一个建议是在curl_close ( $ch );

中的return之前file_get_contents_curl()

我已更新您的代码,以便能够以块的形式工作,而不是一次执行所有执行。我创建了一个表import_progress,我们将存储最后导入/执行的行,并为每次执行获取20行:

/**
 * Get the last imported ID
 *
 * @param mysqli $con
 *
 * @return int
 */
function get_last_inserted_id( $con ) {
    $sql    = "SELECT imported_id
          FROM import_progress
          ORDER by ID desc
          LIMIT 1";

    $result = $con->query( $sql );
    if ( $result->num_rows == 0 ) {
        //Since you are fetching in descending order
        //We need to return a very high number if we did not import anything yet
        return 999999999999999999;
    } else {
        while ( $row = $result->fetch_assoc() ) {
            return $row['imported_id'];
        }
    }
}

/**
 * Get the last imported ID
 *
 * @param mysqli $con
 * @param array  $ids
 *
 * @return int
 */
function insert_imported_ids( $con, $ids ) {
    $ids    = array_unique( array_filter( $ids ) );
    if ( empty( $ids ) ) {
        return false;
    }

    $sql    = "INSERT INTO import_progress (imported_id) VALUES";

    //We now append the data to the mysql string
    $insert = [];
    foreach ( $ids as $id ) {
        $insert[] = "($id)";
    }
    $sql    .= implode( ',', $insert ) . ';';

    return $con->query( $sql );
}

/**
 * @param string $url
 *
 * @return mixed
 */
function file_get_contents_curl( $url ) {
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HEADER, 0 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 2000000 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 200000 );
    curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
    $data = curl_exec( $ch );
    curl_close( $ch );

    return $data;
}

$user       = "5aabf73bdd2c7";
$servername = "db";
$username   = "root";
$password   = "root";
$dbname     = "stack_overflow";
// Create connection
$con = new mysqli( $servername, $username, $password, $dbname );
// Check connection
if ( $con->connect_error ) {
    die( "Connection failed: " . $con->connect_error );
}

$last_inserted_id = get_last_inserted_id( $con );
$sql = "SELECT link, id, cookie FROM rawlinks 
        WHERE cookie='$user'
        AND ID < $last_inserted_id
        ORDER by ID desc
        LIMIT 20";

$result = $con->query( $sql );

if ( $result->num_rows > 0 ) {
    $imported_ids = [];
    // output data of each row
    while ( $row = $result->fetch_assoc() ) {
        $getimagefrom = $row["link"];
        $id           = $row["id"];
        $cookie       = $row["cookie"];
        echo $getimagefrom;
        echo "<br><br>ID:" . $id . "<br><br>";
        $htmlaa = file_get_contents_curl( $getimagefrom );
        $docaa  = new DOMDocument();
        @$docaa->loadHTML( $htmlaa );
        $nodesaa  = $docaa->getElementsByTagName( 'title' );
        $nodesxaa = $docaa->getElementsByTagName( 'img' );
        //get and display what you need:
        $titlev = $nodesaa->item( 0 )->nodeValue;
        $metasv = $docaa->getElementsByTagName( 'meta' );
        $asv    = $docaa->getElementsByTagName( 'img' );
        for ( $iv = 0; $iv < $metasv->length; $iv ++ ) {
            $metav = $metasv->item( $iv );
            if ( $metav->getAttribute( 'name' ) == 'description' ) {
                $descriptionv = $metav->getAttribute( 'content' );
            }
            if ( $metav->getAttribute( 'name' ) == 'keywords' ) {
                $keywordsv = $metav->getAttribute( 'content' );
            }
            if ( $metav->getAttribute( 'property' ) == 'og:image' ) {
                ;
            }
            $languagev = $metav->getAttribute( 'content' );
        }

        for ( $ivv = 0; $ivv < $asv->length; $ivv ++ ) {

            $av = $asv->item( $ivv );
            echo $av->getAttribute( 'src' );
            echo "<br>";
            $cvarv = $av->getAttribute( 'src' );
            echo " <img src='$cvarv' >";
            $cvarv        = urlencode( $cvarv );
            $titlev       = preg_replace( '/[^\p{L}\p{N}\s]/u', '', $titlev );
            $descriptionv = preg_replace( '/[^\p{L}\p{N}\s]/u', '', $descriptionv );

        }

        $imported_ids[] = (int)$id;
    }
    insert_imported_ids($con, $imported_ids);
}
$con->close();
echo "FINISHED!!!!";

答案 1 :(得分:0)

Curl没有任何区别。如果set_time_limit不起作用,则意味着全局配置会覆盖它,请尝试打印以下内容:

var_dump(ini_get('max_execution_time'));

执行set_time_limit(0);之前以及完成之后的一次。

这可以由php.ini编辑,但通常也是免费或共享托管服务提供商的阻止功能,以防止用户过载共享服务器。

答案 2 :(得分:0)

添加

ini_set('max_execution_time', 500);

在页面顶部,这里500是秒数。