PHP内存使用情况和限制,同时存储多个变量

时间:2018-10-13 17:03:03

标签: php memory

一旦从curl_multi_init会话中收到许多变量,我将立即定义/存储它们:

$response_[$deger] = curl_multi_getcontent($ch_[$deger]);

$deger从0循环到200时。$ deger达到〜80时,它将停止存储响应(例如,从$response_[81]开始)。

PS:每次迭代都会从Web服务获取一个小的xml(3-4 KB)。

我认为这是php的内存问题。我该如何解决这个问题?

非常感谢,

穆拉特

原始帖子的添加:

关于@Anton的建议,这是我的完整代码:

<?php

ini_set('memory_limit', '4096M'); // after facing problem I put this line to test

$yazi="12345"; // in my entire code $yazi is received as a post parameter.

$curl       = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "http://example.com/WebService.asmx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n  <soap:Header>\r\n    <AuthHeader xmlns=\"http://tempuri.org/\">\r\n      <Username>myUserName</Username>\r\n      <Password>myPassWord</Password>\r\n    </AuthHeader>\r\n  </soap:Header>\r\n  <soap:Body>\r\n    <SearchByNumber xmlns=\"http://tempuri.org/\">\r\n      <no>$yazi</no>\r\n    </SearchByNumber>\r\n  </soap:Body>\r\n</soap:Envelope>",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: text/xml",
    "Postman-Token: 3ebc4ee5-3836-4fd6-85a7-e2fb652e63da"
  ),
));

$response   = curl_exec($curl);
//$err      = curl_error($curl);

curl_close($curl);

$domDocument    = new DOMDocument();
$domDocument->loadXML($response);

$results    = $domDocument->getElementsByTagName("NumberSearch");
$markas     = $domDocument->getElementsByTagName('Marka');
$parcanos   = $domDocument->getElementsByTagName('ParcaNo');
//        there are some more 5-6 elements here, 

$i=0;
$couples = [];
foreach ($results as $result) {
    $marka      = $markas->item($i)->nodeValue;
    $parcano    = $parcanos->item($i)->nodeValue;

    $couples[ $i ]  = array(
            "brand" => $marka,
            "sku"   => $parcano
            );
            $i++;
        }

for ( $deger = 0; $deger < $i; $deger++ ) { // $deger reaches 188 in my example.
    $brand=$couples[$deger][brand];
    $sku=$couples[$deger][sku];
    $ch_[$deger] = curl_init();
    curl_setopt_array($ch_[$deger], array(
          CURLOPT_URL => "http://example.com/WebService.asmx",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n  <soap:Header>\r\n    <AuthHeader xmlns=\"http://tempuri.org/\">\r\n      <Username>myUserName</Username>\r\n      <Password>myPassWord</Password>\r\n    </AuthHeader>\r\n  </soap:Header>\r\n  <soap:Body>\r\n    <GetCrits xmlns=\"http://tempuri.org/\">\r\n      <brand>$brand</brand>\r\n      <no>$sku</no>\r\n    </GetCrits>\r\n  </soap:Body>\r\n</soap:Envelope>",
          CURLOPT_HTTPHEADER => array(
            "Cache-Control: no-cache",
            "Content-Type: text/xml",
            "Postman-Token: 98646c47-59da-4c0a-bda6-af241ff3e14e"
          ),
        ));
    }

$mh = curl_multi_init();

for ( $deger = 0; $deger < $i; $deger++ ) {
    curl_multi_add_handle($mh, $ch_[$deger]);
    }

  $running = null;
  do {
        curl_multi_exec($mh, $running);
        } while ($running);

for ( $deger = 0; $deger < $i; $deger++ ) {
    $response_[$deger] = curl_multi_getcontent($ch_[$deger]);

    //*****************************************************************
    //************ I THINK MY PROBLEM OCCURS AT THIS POINT ************
    //*****************************************************************

    curl_multi_remove_handle($mh, $ch_[$deger]);
    }

curl_multi_close($mh);

    for ( $deger = 0; $deger < $i; $deger++ ) {
        $domDocument    = new DOMDocument();
        $domDocument->loadXML($response_[$deger]);
        $z          = 0;
        $results        = $domDocument->getElementsByTagName("GetCrits");
        $markas         = $domDocument->getElementsByTagName('Marka');
        $parcanos       = $domDocument->getElementsByTagName('ParcaNo');
        $siranos        = $domDocument->getElementsByTagName('SiraNo');
        //            +7 more elements here...

        foreach($results as $result) {
            $marka      = $markas->item($z)->nodeValue;
            $parcano    = $parcanos->item($z)->nodeValue;
            $sirano     = $siranos->item($z)->nodeValue;
            //        +7 more elements here...

            $z++;
            echo $marka."-".$parcano."-".$sirano...."<br>";
            }
        }

$endMemory = memory_get_usage();
echo "<br>end memory: $endMemory <br>";

?>

0 个答案:

没有答案