I am writing PHP code to send XML string data to a site using HTTP post with the encType: multipart/form-data encoding. I am thinking of using PHP's ht开发者_高级运维tp_post_data function. Before sending the data, I believe I need to encode it, but I cannot see a PHP function to do this for me, nor do I know how to write such a function myself.
This is what I have so far (but http_request_body_encode() is certainly not the correct function):
$options = array('headers' => array('Content-Type' => 'multipart/form-data'));
$fields = array('operation' =>'doMDUpload', 'login_id' => $doi_username, 'login_passwd' => $doi_password,
'area' => ($debug ? "test" : "live"), 'fname' => $writer->outputMemory());
$info = array();
$response = http_post_data($crossref_deposit_url, http_request_body_encode($fields, array()), $options, $info);
$writer->outputMemory
is an XML string containing bibliometric data.
Well, http_request_body_encode() would be the correct function. But it is not a core function. It's available with the http pecl extension, that's not even installed per default on PHP 5.3.
It also does not generate a multipart/form-data
per default (instead x-www-form-urlencode). You can trick it by supplying a fake $files
array however:
print http_request_body_encode(array("field"=>123), array(NULL=>NULL));
Now the second problem is that you need to cut out the first line. That's the Content-Type
with the correct boundary=
attribute. And that seems fiddly to me.
So better use the HttpRequest class directly, and let it handle the POST encoding and sending at once. Alternatively use the PEAR or Zend class, which also work regardless of presence of the HTTP extension module.
精彩评论