0

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;
}
Sujay
  • 2,510
  • 2
  • 27
  • 47
  • 1
    So what debugging steps have you undertaken so far? What debug outputs have you made where, and with what results? – CBroe Jul 24 '15 at 21:14
  • _“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”_ – so you are assembling one JSON data structure on the receiving side again, from the data send via the multiple cURL handles, and they all go to the same “receiver”? Where is the code that handles that part? – CBroe Jul 24 '15 at 21:18
  • I found the problem. I was sending a complex array which had to be json encoded. But what I didn't know is that not all the array could be encoded. There has to be a key to what you send through curl which I resolved by only json encoding after the second dimension of the array and using the function http_build_query(). This solved my problem. Thank you for helping! – José Camilo Aug 03 '15 at 03:05

0 Answers0