How to send upload file to other website by using curl in php?

前端 未结 2 429
栀梦
栀梦 2021-01-16 12:07

How does one upload a file to another website by using Curl in PHP and get the response page?

The website: http://www.postto.me

相关标签:
2条回答
  • 2021-01-16 12:15

    On top of Paul Schreiber's answer:

    According to how to upload file using curl with php, starting from php 5.5 you need to use curl_file_create($path) instead of "@$path".

    Tested: it does work.

    With the @ way no file gets uploaded.

    0 讨论(0)
  • 2021-01-16 12:34

    Here's an example of how to upload a file. (First hit for "curl file upload example php".)

    <?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, _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);
    ?>
    
    0 讨论(0)
提交回复
热议问题