I am defining/storing many variables at once as soon as they are received from a curl_multi_init
session:
$response_[$deger] = curl_multi_getcontent($ch_[$deger]);
while $deger
is looping from 0 to 200. As soon as $deger reaches ~80, it stops storing response (from $response_[81]
for example).
PS: Every iteration gets a small xml (3-4 kilobytes) from web service.
I think it is a memory issue of php. How can I handle this problem?
Many thanks,
Murat
ADDITION TO ORIGINAL POST:
Regarding @Anton 's advice, here is my entire code:
<?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\"?>
<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/\">
<soap:Header>
<AuthHeader xmlns=\"http://tempuri.org/\">
<Username>myUserName</Username>
<Password>myPassWord</Password>
</AuthHeader>
</soap:Header>
<soap:Body>
<SearchByNumber xmlns=\"http://tempuri.org/\">
<no>$yazi</no>
</SearchByNumber>
</soap:Body>
</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\"?>
<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/\">
<soap:Header>
<AuthHeader xmlns=\"http://tempuri.org/\">
<Username>myUserName</Username>
<Password>myPassWord</Password>
</AuthHeader>
</soap:Header>
<soap:Body>
<GetCrits xmlns=\"http://tempuri.org/\">
<brand>$brand</brand>
<no>$sku</no>
</GetCrits>
</soap:Body>
</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>";
?>