1

I'm trying to hit the Census Geocoding API via Curl in PHP. Here's (a trimmed down version of) my code:

$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);

This results in getting an error back from the 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."

However, if I invoke Curl form the command line, it works fine...

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

Any idea what's going wrong here?

John Chrysostom
  • 3,623
  • 1
  • 28
  • 45
  • If one works and the other does, compare the headers sent to determine what is missing. You can add `-v` on the command line and [read the highest voted answer](http://stackoverflow.com/questions/3164405/show-curl-post-request-headers-is-there-a-way-to-do-this) for PHP. – Jason McCreary Jan 19 '15 at 15:24

1 Answers1

1

You may be on PHP version 5.6, in which case the @ feature is deprecated and turned off by default. It can still be enabled by explicit curl_setopt setting:

curl_setopt($curl_handle, CURLOPT_SAFE_UPLOAD, false);
Hans Z.
  • 41,402
  • 9
  • 80
  • 105
  • 1
    Wow, thanks. I already switched to using `curl_file_create`, which I guess is now the PHP 5.6-approved way of doing things... but this explains why I needed to! – John Chrysostom Jan 19 '15 at 17:06