hi all
How can i use Http_client module to POST binary files to server that have resource to accept files ?
here my try :
$file_tmp_uri = mica_studies_copy_attachment_file($attachement);
$url = 'http://localhost:8082/ws/files/temp';
$handle = fopen($file_tmp_uri, "r");
$openedfile = fread($handle, filesize($file_tmp_uri));
fclose($handle);
$post = new HttpClientRequest($url, array(
'method' => POST,
'headers' => array('Content-Type' => array('multipart/form-data'), 'Content-Length'=>array(filesize($file_tmp_uri))),
'data' => array('file'=> $openedfile),
'parameters' => array(),
));

$client = new HttpClient($formatter = FALSE);
$body = $client->execute($post);

-------------------server receive :
6 > POST http://localhost:8082/ws/files/temp
6 > Accept: */*
6 > Content-Length: 117262,5
6 > Content-Type: multipart/form-data
6 > Host: localhost:8082
6 > User-Agent: Drupal (+http://drupal.org/)
------------------------server response :
message: "HTTP 400 Bad Request"
messageTemplate: "error.unhandledException"

------------------this code work fine :

$file_tmp_uri = mica_studies_copy_attachment_file($attachement);
$file_info = new finfo(FILEINFO_MIME);
$mime_file = $file_info->file($file_tmp_uri);
$url = 'http://localhost:8082/ws/files/temp';
$cfile = new CurlFile($file_tmp_uri, $mime_file, $attachement['filename']);
$data_file = array('file' => $cfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HEADER, TRUE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($resource, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $data_file);

$result = curl_exec($resource);

$http_code = curl_getinfo($resource, CURLINFO_HTTP_CODE);
$http_header_out = curl_getinfo($resource);
if (preg_match('/(?<=files\/temp\/).*/', $result, $group)) {
$match = $group[0];
}
curl_close($resource);
-------------------------------------------------
what i do wrong
thanks ;)

Comments

SamirMtl’s picture

Issue summary: View changes
SamirMtl’s picture

Category: Support request » Feature request