Empty reply with curl post request

Hello All,

We tried to use curl to make HTTP post request with json data to internal micro-service which use Slim framework. But curl returns with empty reply randomly. Even in that case, the micro-service got the request and handles it successfully.
And we’re getting same issues with/without apache mod_rewrite engine for url rewriting.

Any help would be appreciated.
Thanks in advance.

  • Service environment

PHP : 5.5.38
Apache : 2.2.3
Slim : 3.7.0

  • Here is the code snippet how we make the curl request in php.

$curl = curl_init();
$encodedData = json_encode($data);
$curlOption = [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => 10,
CURLOPT_POST => 1,
CURLOPT_HEADER => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => [
‘Content-Type: application/json’,
'Content-Length: ’ . strlen($encodedData),
'Expect: ’
];
CURLOPT_POSTFIELDS => $encodedData,
CURLOPT_FRESH_CONNECT => 1
];
curl_setopt_array($curl, $curlOption);
$response = curl_exec($curl);
if ($response === false) {
$error = curl_error($curl);
$curlInfo = curl_info($curl);
error_log("error : ". $error);
error_log("curl_info : " . var_export($curInfo, true);
} else {
// handle response
}

  • microservice directory structure

/microservice/service1/lib/slim => slim framework
/microservice/service1/www/html => web document root
api.php
.htaccess

  • Below is curl verbose result when we got empty reply from microservice.

Trying x.x.x.x…
Connected to x.x.x.x (x.x.x.x) port 80 (#0)
POST /service/www/html/api.php/abc HTTP/1.1
Host: x.x.x.x
Accept: /
Content-Type: application/json
Content-Length: 31006
upload completely sent off: 31006 out of 31006 bytes
Empty reply from server
Connection #0 to host x.x.x.x.com left intact

  • error string and dump of curl_info.

error : Empty reply from server
curl_info : array (
‘url’ => ‘http://x.x.x.x/api.php/abc’,
‘content_type’ => NULL,
‘http_code’ => 0,
‘header_size’ => 0,
‘request_size’ => 31180,
‘filetime’ => -1,
‘ssl_verify_result’ => 0,
‘redirect_count’ => 0,
‘total_time’ => 0.143499999999999960920149533194489777088165283203125,
‘namelookup_time’ => 0.038522000000000000685673740008496679365634918212890625,
‘connect_time’ => 0.04020300000000000262279087337446981109678745269775390625,
‘pretransfer_time’ => 0.040597000000000001140865180104810860939323902130126953125,
‘size_upload’ => 31006,
‘size_download’ => 0,
‘speed_download’ => 0,
‘speed_upload’ => 216069,
‘download_content_length’ => -1,
‘upload_content_length’ => 31006,
‘starttransfer_time’ => 0.14344699999999999118216464921715669333934783935546875,
‘redirect_time’ => 0,
‘redirect_url’ => ‘’,
‘primary_ip’ => ‘x.x.x.x’,
‘certinfo’ =>
array (
),
‘primary_port’ => 80,
‘local_ip’ => ‘x.x.x.x’,
‘local_port’ => x,
)
error : Empty reply from server

Are you sure that your mircoservice accepts POST calls?

Every post request had been logged in the micro-service correctly.

Try CURLOPT_FAILONERROR and see what it outputs?

Tried CURLOPT_FAILONERROR but it didn’t output any difference.

So microservice logs fine, what does your responce look like?
Its most likely not a curl problem so it must be either your service or server enviroment.

Expects to get json type data in response but got empty reply randomly. In that case, http status code is 0.
I’m suspicious of server env as well. Any suggestions to debug the issue?

And tried to make the post request with same data to old microservice (same server env) without Slim framework but didn’t see the same issue.

If the same code works fine without using the Slim framework then perhaps you are doing something wrong.
Would be usefull to see some code as you use it in the Slim service.

Code snippet of the microservice with the Slim Framework. It’s the simple post request so we didn’t do much things with the Slim framework except the URL rewriting.

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require_once DIR.‘/…/…/lib/slim/vendor/autoload.php’;
require_once DIR.‘/…/…/service1/init.php’;

$app = new \Slim\App();

$app->post(‘/xxxx’, function (Request $request, Response $response)) {
$requestBody = $request->getParsedBody();
$api = new ServiceApi();
$apiResponse = $api->doSomething($requestBody[‘data’]);
return $response->withJson($apiResponse, 200);
});

$app->run();

Have you checked that $requestBody[‘data’] contains the required JSON data?
You should always check for the existence of expected data.

Also try this:
$response = $response->withJson($YourJsonData);
return $response;

Yes. it contains the required JSON data. And tried your suggestion but it didn’t make any difference.

By the way, updated apache log level to debug and noticed that it logs “[notice] child pid xxxxx exit signal Aborted (6)” whenever the curl post request got empty response. May it be apache issue?

Could be, maybe test on a different enviroment?
Send me a message in private (not sure if that is possible here).

Zend Opcache with fast_shutdown seems to cause memory corruption so the child process dies with signal abort or segmentation fault.
After turning off the option (opcache.fast_shutdown=0), the issue were gone.