【问题标题】:How to send upload file to other website by using curl in php?如何在 php 中使用 curl 将上传文件发送到其他网站?
【发布时间】:2011-04-12 00:03:14
【问题描述】:

如何在 PHP 中使用 Curl 将文件上传到另一个网站并获取响应页面?

网站:http://www.postto.me

<form action="posttome.php" enctype="multipart/form-data" method="post">
<input type="hidden" value="2097152" name="MAX_FILE_SIZE">
<input type="file" size="30" class="input" name="img">
<input type="submit" class="input" value="Upload" name="upload">
</form>


<?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, "http://www.postto.me/upload.php");
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "img"=>$_FILES["img"]["tmp_name"],
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    echo $response;

echo $_FILES["img"]["tmp_name"];
}
?>

它不工作[如何]??

【问题讨论】:

  • 你试过阅读 PHP.net 上的cURL manual 吗?
  • 我不知道如何和什么,但这对我有用:) 唯一的区别是我不将文件上传到我的服务器,而是将默认文件发送到站点

标签: php curl


【解决方案1】:

在 Paul Schreiber 的回答之上:

根据how to upload file using curl with php,从php 5.5开始你需要使用curl_file_create($path)而不是"@$path"

经过测试:确实有效。

使用@ 方式不会上传任何文件。

【讨论】:

    【解决方案2】:

    这是example of how to upload a file。 (首先点击“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);
    ?>
    

    【讨论】:

    • 在另一个网站上工作? "file_box"=>"@/path/to/myfile.jpg", ?
    猜你喜欢
    • 2016-07-20
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2011-09-04
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多