使用php和cURL转发多部分表单数据


Fowarding a multipart form data with php and cURL

我正在用cURL开发一个PHP小代理。它必须从客户端接收http请求,对这些请求进行统计,将请求转发给web服务器,并将响应转发给客户端。GET请求和POST请求与文本/html数据的一切工作正常。

我有问题与多部分/form-data数据转发请求,特别是我有$_POST和$_FILES全局变量内的数据。

我应该如何使用这两个变量将请求转发到服务器?

首先需要将文件存储在服务器中,然后使用如下代码:

<?php
    $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, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>