Is it possible to use cURL to stream upload a file using POST?

后端 未结 2 1972
花落未央
花落未央 2020-12-31 18:41

Or does PHP not allow this? I have read it is possible using PUT but the server is expecting POST only.

相关标签:
2条回答
  • 2020-12-31 18:47

    Yes it is possible to stream upload a file using POST file uploads with cURL. This is the default and you need to provide the filename of the file(s) you would like to stream in form of strings.

    // URL on which we have to post data
    $url = "http://localhost/tutorials/post_action.php";
    // Any other field you might want to catch
    $post_data['name'] = "khan";
    // File you want to upload/post
    $post_data['file'] = "@c:/logs.log";
    
    // Initialize cURL
    $ch = curl_init();
    // Set URL on which you want to post the Form and/or data
    curl_setopt($ch, CURLOPT_URL, $url);
    // Data+Files to be posted
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    // Pass TRUE or 1 if you want to wait for and catch the response against the request made
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // For Debug mode; shows up any error encountered during the operation
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    // Execute the request
    $response = curl_exec($ch);
    
    // Just for debug: to see response
    echo $response;
    

    Apart from that default method, it is not possible to use cURL to stream upload a file using the POST method.

    0 讨论(0)
  • 2020-12-31 19:03

    Curl has already support the stream, try curl --help | grep binary and you will get:

    "--data-binary DATA HTTP POST binary data (H)"

    An example: curl -v -XPOST http://example:port/path --data-binary @file.tar \ -H "Content-Type: application/octet-stream"

    0 讨论(0)
提交回复
热议问题