简体   繁体   中英

Forward multi-part POST to another page

Okay, I have two sites, we'll call them A and B. Site A has a mutli-part form with a file attachment; Site B has a similar form (which I'm not going to use) and a processing page that it submits to. Due to browser origin policies, I'm adding a "proxy page" to Site B that I can post to from Site A. The proxy page will then need to "forward" the data to the processing page and get a response which I will use to generate JSONP for sending back to Site A.

I'd normally use the session or just generate and autosubmit a form on the proxy page but I'm not sure how to do this since it involves a file input tag. I'm thinking I may be able to use something like this: http://www.php.net/manual/en/function.httprequest-send.php

Any ideas?

It'd be relatively trivial with curl.

$post_data = $_POST; // copy over the non-file post data
foreach($_FILES as $key => $filedat) {
   $post_data[$key] = '@' . $filedat['tmp_name'];  // add the uploaded files to the new field list
}

$ch = curl_init('http://siteb.com/proxy_page')
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$res = curl_exec($ch);

This will undoubtedly need some tweaking/fixing, but should be enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM