Hi,
I'm developing the ipaper module (http://drupal.org/project/ipaper), which does 3rd party integration with the www.scribd.com API. They have a great document display platform and the module I wrote is very comprehensive: it allows you to upload documents to scribd from your website, displays them using their ipaper application and even fetches full text for your own search index and thumbnails for your teaser views. Please help with testing if you are curious at all.
I want to finish one more functionality before I release a beta version and I need your help. I have an API call were I need to HTTP POST a Drupal file upload to scribd.com. It works well through CURL with having a parameter like 'file' => '@/home/drupal/files/file.txt', but I also want to offer support for those who don't have CURL.
I am trying to do it with drupal_http_request(), but I'm not even having success the non-file parameters across. Can anyone help me construct a POST with text (api_key, method) as well as binary (file) parameters? I understand that in order to do that I need to construct a 'multipart/form-data' request?
Thanks,
Rareş
/**
* Provides for increased compatibility by sending requests either through CURL or fsockopen(drupal_http_request)
*/
function _ipaper_request($request_url, $params = NULL, $method = 'GET'){
if (function_exists("curl_init")){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == 'POST')
curl_setopt($ch, CURLOPT_POST, 1 );
if ($params)
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$data = curl_exec( $ch );
curl_close($ch);
return $data;
}
else{
$headers = array();
// ??? $headers = array('Content-Type' => 'multipart/form-data');
$request = drupal_http_request($request_url . http_build_query($params), $headers, $method );
// or maybe: $request = drupal_http_request($request_url, $headers, $method, http_build_query($params));
if ($request->error)
watchdog("ipaper", "Request failed - ". $request->error .' - '. http_build_query($params));
return $request->data;
}
}
Comments
Content-Type
I think you need to set Content-Type to
application/x-www-form-urlencoded
.Also, according to the HTML specification, space should be encode as
+
rather than%20
, and newlines should be encoded as%0D%0A
rather than just%0A
, but I am not sure it matters in practice.Here's what I came up with
--Rareş
Thanks
Almost 4 years later, the syntax of
drupal_http_request($url, $options)
has changed a little, but your code example is still very useful. Thanks!Update for Drupal 7
I was looking for a solution and this post really helped me out. I think that a working example for D7 could help other people.
I had a form with 4 fields (text, select boxes and textarea) plus an attachment field. Here is my code:
and how to use these
and how to use these wonderful functions?
HTTPRL
I just added support for sending files in HTTPRL. Example is in the issue queue: http://drupal.org/node/1780562#comment-6710636