CURL 错误发布文件

CURL Errors POSTing File

我正在尝试通过 PHP 中的 Curl 进行人口普查地理编码 API。这是我的代码(精简版):

$post = array('benchmark' => 'Public_AR_CENSUS2010', 'addressFile'=>'@C:\TestData.csv');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://geocoding.geo.census.gov/geocoder/locations/addressbatch/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);

echo($result);
curl_close ($ch);

这导致从 API:

返回错误
"From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1: 10.4.1 400 Bad Request. The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications."

但是,如果我从命令行调用 Curl,它工作正常...

curl --form addressFile=@"C:\TestData.csv" --form benchmark=PUBLIC_AR_CENSUS2010 http://geocoding.geo.census.gov/geocoder/locations/addressbatch

知道这里出了什么问题吗?

您可能使用的是 PHP 5.6 版,在这种情况下,@ 功能已弃用并默认关闭。它仍然可以通过显式 curl_setopt 设置启用:

curl_setopt($curl_handle, CURLOPT_SAFE_UPLOAD, false);