在PHP中通过cURL发送多字节数据

时间:2012-12-06 09:54:34

标签: php api curl

我正在为我正在为客户编写的应用程序创建API,因为他们想要一个网站和一个移动应用程序。 API和应用程序使用Codeigniter框架以PHP编写。

我想使用输入文件通过应用程序上传文件。然后我如何通过cURL将此文件发送到API,以便我可以在$ _FILES变量中获取该文件?

理想情况下,我想使用POST方法,但如果需要,我也可以使用PUT或类似方法。

由于

取值

=== UPDATE ===

我知道如何将文件从应用程序传递给API - 我的问题的重点更多的是如何在API中解析文件。如果您使用表单上传文件,则会在$ _FILES中获得临时文件引用 - 这就是我想要模拟的内容。

2 个答案:

答案 0 :(得分:0)

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

&GT;

reference :: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

答案 1 :(得分:0)

在文件路径前附加'@'字符以上传文件。

在您的情况下,请使用输入文件中的tmp_name。

假设传入的文件是通过POST发送的,名称为“input_file”。

$curl = curl_init(/* Your URL goes here. */);

curl_setopt_array($curl, array(
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => array(
        'file' => "@$_FILES[input_file][tmp_name]"
    )
));

curl_exec($curl);

希望有所帮助。