PHP脚本内存优化

时间:2015-06-10 22:23:33

标签: php

下面有一个脚本,它会检查与外部网站API相关的35000个ID号。

脚本似乎过早地没有错误地结束,看了之后我觉得它可能与内存有关。

现在我可以重新运行脚本几次,我最终会获得所有信息,但我将来也不会这样做。

<?php
    /**
     * Widgets Module
     * 
     * This script was designed to run once a week. It downloads item information and resources from divine-pride.net.
     * The information is systematically added to a database that is queried by the index.php in the root folder; this is the file used to pull widget information.
     *
     * PHP version 5
     *
     * @author     Michael McStay <mjm668@gmail.com>
     * @copyright  2014-2015 crmIndexer
     * @version    1.0.0
     * @link       http://crmindexer.org
     */
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require_once('helper.php');

    $conn = mod_adm_helper::connect_to_db("localhost", "root", "", "mamajamatempname"); // local connection

    if ($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    }

    $stmt = $conn->prepare("
        SELECT `id` 
        FROM `items`
    ");

    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($id);
    $list_of_existing_items = array();

    while ($stmt->fetch())
    {
        array_push($list_of_existing_items, $id);
    }

    for ($i = 1; $i <= 35000; $i += 1)
    {
        if(!in_array($i, $list_of_existing_items))
        {
            $obj = file_get_contents("http://www.divine-pride.net/api/database/Item/" . $i . "?apiKey=XYZ123");
            $obj = json_decode($obj);

            if(!is_null($obj))
            {
                mod_adm_helper::add_item($obj->id, $obj->name, $obj->description, $obj->slots, $obj->itemTypeId, $obj->itemSubTypeId, $obj->location, $conn);
                mod_adm_helper::upload_image(file_get_contents("http://static.divine-pride.net/images/items/item/" . $obj->id . ".png"), "item", $obj->id);
                mod_adm_helper::upload_image(file_get_contents("http://static.divine-pride.net/images/items/collection/" . $obj->id . ".png"), "collection", $obj->id);
            }
        }
        else
        {
            echo "#" . $i . " is already a part of the database<br />";
        }
    }
?>

以下是添加项目代码:

        public static function add_item($id, $name, $description, $slots, $itemtypeid, $itemsubtypeid, $location, $conn)
    {
        $stmt = $conn->prepare("
            INSERT INTO `items` (`id`, `name`, `description`, `slots`, `itemtypeid`, `itemsubtypeid`, `location`) 
            VALUES (?, ?, ?, ?, ?, ?, ?)
        ");

        $pattern = "/\^([a-fA-F0-9]{6})(.*?)\^([0]{6}|[0]{5}|[0]{4}|[0]{3}|[0]{2}|[0]{1})/";
        $description = preg_replace($pattern, "<span style=\"color:#$1\">$2</span>", str_replace("\r\n", "<br />", $description));

        $stmt->bind_param("sssssss", $id, $name, $description, $slots, $itemtypeid, $itemsubtypeid, $location);
        $stmt->execute();
    }

这是上传图片代码:

        public static function upload_image($object, $directory, $newfilename)
    {
        file_put_contents("../img/items/" . $directory . "/" . $newfilename . ".png", $object);
        return true;
    }

如果有任何帮助可以帮助我找到改进这个脚本以便长时间运行或占用更少内存的方法,我非常擅长处理数据库中已经存在的数据集的脚本。< / p>

0 个答案:

没有答案