I'm sending parallel requests using curl_multi_init
, curl_multi_add_handle
, curl_multi_exec
, curl_multi_select
, curl_multi_getcontent
, curl_multi_remove_handle
and curl_multi_close
. The data is perfect when I do the json_encode() but after I send it to the other server, the json array comes with an aditional null value at the end that makes the system break. Looks like the curl muti functions are injection a null value at the end. Has anyone had this problem before or knows the answer to this?
The code goes like this:
public function curlMulti($data)
{
$multi = curl_multi_init();
$channels = array();
// Loop through the array, create curl-handles
// and attach the handles to our multi-request
foreach ($data as $oneData) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Token token="' . $this->_api_token . '"'
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, 1);
$oneData = json_encode($oneData);
curl_setopt($ch,CURLOPT_POSTFIELDS, $oneData);
curl_multi_add_handle($multi, $ch);
$channels[] = $ch;
}
// While we're still active, execute curl
$active = null;
do {
$mrc = curl_multi_exec($multi, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
// Wait for activity on any curl-connection
if (curl_multi_select($multi) == -1) {
continue;
}
// Continue to exec until curl is ready to
// give us more data
do {
$mrc = curl_multi_exec($multi, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
// Loop through the channels and retrieve the received
// content, then remove the handle from the multi-handle
foreach ($channels as $channel) {
$response[] = curl_multi_getcontent($channel);
curl_multi_remove_handle($multi, $channel);
}
foreach($response as $key => $oneResponse) {
$response[$key] = json_decode($oneResponse, true);
}
// Close the multi-handle and return our results
curl_multi_close($multi);
return $response;
}