卷曲错误:从服务器清空回复

I'm trying to do a CURL Post and running into some problems.

Essentially I'm posting to an API a list of zip codes, and depending on what is selected, it could potentially be a very large list. When I post only a few zip codes, it works perfect. When I throw in a larger list of zip codes, it fails with the Empty Reply From Server error.

private function sendApiRequest($action, $request)
{
    // Testing
    $request['Test'] = $this->apitest;

    // Build URL
    $postURL = "{$this->apiurl}?Key={$this->apikey}&API_Action={$action}";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $postURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $resp = curl_exec($ch);

    if(curl_errno($ch))
    {
        var_dump(curl_getinfo($ch));
        throw new Exception('Curl Error: ' . curl_error($ch));
    }
    curl_close($ch);
    return $resp;
}

Here is the curl_getinfo() dump:

array
  'url' => string 'https://api.example.com/api.php?Key=xxx&API_Action=insertFilterSet' (length=147)
  'content_type' => null
  'http_code' => int 0
  'header_size' => int 0
  'request_size' => int 11736
  'filetime' => int -1
  'ssl_verify_result' => int 20
  'redirect_count' => int 0
  'total_time' => float 0.374
  'namelookup_time' => float 0
  'connect_time' => float 0.093
  'pretransfer_time' => float 0.203
  'size_upload' => float 11486
  'size_download' => float 0
  'speed_download' => float 0
  'speed_upload' => float 30711
  'download_content_length' => float -1
  'upload_content_length' => float 11486
  'starttransfer_time' => float 0.374
  'redirect_time' => float 0
  'certinfo' => 
    array
      empty
  'redirect_url' => string '' (length=0)

As I said, it works perfect for smaller requests, but errors out on large requests.

I came across the same issue. My HTTP status code returned 200 but my response was empty. There could be many reasons for this as I experienced.

Your headers might be incorrect

CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Expect:')

You might need to send data as post fields in CURL and not attached to the URL like url?p1=a1&p2=a2

$data = array (p1=>a1, p2=>a2)
CURLOPT_POSTFIELDS => $data

So your options array would be similar to the following

array(
    CURLOPT_URL => $url,
    CURLOPT_FAILONERROR => TRUE, // FALSE if in debug mode
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Expect:'),
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $data,
);