如何发送上传文件到其他网站通过使用curl在PHP? [英] How to send upload file to other website by using curl in php?

查看:126
本文介绍了如何发送上传文件到其他网站通过使用curl在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过在PHP中使用Curl将文件上传到其他网站并获取响应页面?

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

网站: 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"];
}
?>

it not working [How] ??

it not working [How]??

推荐答案

这是一个如何上传文件的示例。 (curl file upload example php的第一个匹配。)

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);
?>

这篇关于如何发送上传文件到其他网站通过使用curl在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆