我正在尝试做一个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)
正如我所说,它适用于较小的请求,但在大量请求时出错。
答案 0 :(得分:0)
我遇到了同样的问题。我的HTTP状态代码返回200,但我的回复是空的。我经历过这可能有很多原因。
CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Expect:')
url?p1=a1&p2=a2
$data = array (p1=>a1, p2=>a2)
CURLOPT_POSTFIELDS => $data
因此您的选项数组将类似于以下
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,
);