Reputation: 5522
I'm trying to translate a curl command I was given into a command I can run through PHP.
The command is:
curl -F customerid=902 -F username=API1 -F password=somepassword -F reportname=1002 http://somerandomurl.com/api/v1/getreportcsv
When I try to run this through PHP (and eventually through C#) however, the web service returns an error. Any idea what could be wrong with my code that is making it error? I think the web service must be very specific about the headers/request:
$url = "http://somerandomurl.com/api/v1/getreportcsv";
$fields = [
"customerid" => "902",
"username" => "API1",
"password" => "somepassword",
"reportname" => "1002"
];
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
print $result;
Wireshark shows the following differences:
The below is the one that works:
POST /somefolder/api/v1/getreportcsv HTTP/1.1
Host: somehost
Accept: */*
Content-Length: 65
Content-Type: application/x-www-form-urlencoded
customerid=902&username=API1&password=somepassword&reportname=1002&HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition 3.1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 6
Date: Thu, 26 Nov 2015 22:51:23 GMT
ERROR
Whereas this one works:
POST /someurl/api/v1/getreportcsv HTTP/1.1
User-Agent: curl/7.33.0
Host: somehost
Accept: */*
Content-Length: 459
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------4b0d14cc31a40c5b
HTTP/1.1 100 Continue
--------------------------4b0d14cc31a40c5b
Content-Disposition: form-data; name="customerid"
902
--------------------------4b0d14cc31a40c5b
Content-Disposition: form-data; name="username"
API1
--------------------------4b0d14cc31a40c5b
Content-Disposition: form-data; name="password"
somepassword
--------------------------4b0d14cc31a40c5b
Content-Disposition: form-data; name="reportname"
1002
--------------------------4b0d14cc31a40c5b--
HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition 3.1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 26 Nov 2015 23:13:57 GMT
2000
...snip... the results of the api
Obviously they are very different requests, however I wouldn't expect something to be so specific?
Upvotes: 0
Views: 313
Reputation: 3236
The question seems very specific to the service in question.
However, the problem might be with headers. According to curl man page:
-F [...] causes curl to POST data using the Content-Type
multipart/form-data
according to RFC 2388
However, according to PHP manual, CURLOPT_POST
option will send data using application/x-www-form-urlencoded
.
According to the same manual, if value of CURLOPT_POSTFIELDS
is an array, the Content-Type header will be set to multipart/form-data
. You may also try to set the content type explicitly as a header.
Try setting the following cURL options:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
If that does not work, it might help analysing all the headers send by the command line curl using the -v
parameter and try to set them. Also might be good idea to set content length header explicitly.
Upvotes: 1