我正在尝试做 CURL Post 并遇到一些问题。
本质上,我正在向 API 发布邮政编码列表,根据选择的内容,它可能是一个非常大的列表。当我只发布几个邮政编码时,它就完美了。当我输入一个更大的邮政编码列表时,它会失败并显示来自服务器的空回复错误。
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;
}
这是 curl_getinfo() 转储:
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)
正如我所说,它适用于较小的请求,但对于较大的请求会出错。