2

I have this code:

<?php
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ERROR | E_WARNING | E_PARSE & ~E_NOTICE);
$uarray = json_decode($_POST['array']);

$uac = $uarray;
$res = $uarray;

function request_callback($response, $info, $request) {
global $uac;
global $res;
$index = array_search($request->{'url'}, $uac);
$uac[$index] = " ";
$rspnc = json_decode($response);
$res[$index] = $rspnc;
}

require("RollingCurl.php");

$rc = new RollingCurl("request_callback");
$rc->options = array(CURLOPT_BINARYTRANSFER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false);
$rc->window_size = 5;
foreach ($uarray as $url) {
$request = new RollingCurlRequest($url);
$rc->add($request);
}
$rc->execute();

for($i = 0; $i <= count($res); $i++)
{
for ($j = 0; $j <= 1; $j++) {
echo $res[$i]->{'name'};
echo "/";
echo $res[$i]->{'quality'};
echo "/";
echo $res[$i]->{'buy_offers'}[$j]->{'o_price'};
echo "/";
echo $res[$i]->{'buy_offers'}[$j]->{'c'};
echo "/";
echo $res[$i]->{'buy_offers'}[$j]->{'my_count'};
echo "/";
echo $res[$i]->{'classid'}. "_"  .$res[$i]->{'instanceid'};
echo "<br>";
}
echo "<p><p><p>";
}
?>

And im getting memory limit error in this string:

$index = array_search($request->{'url'}, $uac);

Array $uarray contains 10000 urls. I have already changed memory_limit value in php.in to -1. phpinfo() displaying memory_limit value as -1. So I have suppose that error occured because of 32bit PHP and Apache. I have windows 64bit with 16GB ram. So problem in the code. Help me rewrite this code especially array_search function in the way that original array will be sliced and merged back or so. Sorry for my language.

el pax
  • 67
  • 2
  • 8
  • [link]http://stackoverflow.com/questions/18942839/php-understanding-memory-limit-error can you please check this.. may be this will help – Tejas Mehta Aug 02 '16 at 13:32
  • You can also try http://www.anindya.com/ At first I would give a try the posted link from Tejas. – Reporter Aug 02 '16 at 13:34

1 Answers1

0

At this case is better to send data as body and read it from php://input by short portions. If every row will be separated by \n (for example), you may read line by line. So, you can avoid to load all data from stream to array $uarray.

For example, you sand request with next body:

[
    {"name":"name1","quality":"1"},
    {"name":"name2","quality":"2"}
]

You can read it line by line:

$handle = fopen('php://input', 'r');
do {
    $str = fgets($handle);
    $data = json_decode($str);
    if ($data) {
        // do what you need
        echo $data->name . PHP_EOL;
        echo $data->quality . PHP_EOL;
    }
} while ($str);
Nick
  • 8,381
  • 6
  • 50
  • 75