开发者

POSTing a multipart/form-data without user intervention in PHP

I need to POST some XML data to a remote server for processing. The server protocol is already defined and unchangeable. The XML data I want to post is dynamically generated within a PHP page running on a local server. I do not want the user to have to save that XML data to a file and then browse for the file to upload it to the remote server (this would not be user-friendly and pointless). My question is how to do this using PHP functions (it would be possible to do it by coding an HTML form with an 'input file=' field, but this means that the user has to browse for the file). I have been given an example, and what I need is to write PHP code that will exactly reproduce the headers in the example and send it to the remote server. Any clues to help me?

POST http://doi.crossref.org/servlet/deposit?  operation=doMDUpload&login_id=USER&login_passwd=PSWD&area=live HTTP/1.1 
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* 
Accept-Language: en-us Content-Type: multipart/form-data; boundary=---------------------------7d22911b10028e 
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461) 
Host: Myhost 
Content-length: 1304 Pragma: no-cache -----------------------------7d22911b10028e 
Content-Disposition: form-data; name="fname"; filename="crossref_query.xml" 
<?xml version="1.0" encoding="UTF-8"?> 

.....XML data....

-----------------------------7d22911b10028e--

Things I have tried without success: a form and a input=hidden field for the XML data (the POSTed data does not include a filename and so is rejected by the remote server); http_post_data() - just gave me an internal server error at the remote server; hand coding the data stream and sending it through a a port 80 socket after using fopensocket - the server didn't recognise that the stream contained POS开发者_运维百科Ted form data, even after checking the stream I sent, character by character using tcpdump. So as you can tell, I'm getting desperate!


from here...

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);


So you're saying the user has a browser window open, and has access to an XML file from a local server (that I assume their browser can access via http), and you want the browser to act as a kind of tunnel to pass that XML file's contents up to your remote server? And the remote server does not have access to that local XML file?

Security issues make this almost impossible to do. (You wouldn't want Yahoo pulling random files off your local network while you browse, right?)

It can only be done if you have some control over the way that the local server provides the XML. In this case, you can leverage JSONP to get around the security issues and allow the page in the browser to submit the data to your remote server as an AJAX post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜