沃梦达 / 编程问答 / php问题 / 正文

在单个 CURL 请求中写入多个文件

Write Multiple files in a single CURL request(在单个 CURL 请求中写入多个文件)

本文介绍了在单个 CURL 请求中写入多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 PHP curl 在单个请求中发送多个文件?

Is there a way using PHP curl to send multiple files in a single request?

我知道您可以使用以下方式发送单个文件:

I understand you can send a single file making use of the following:

$fh = fopen("files/" . $title . "/" . $name, "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);

但我希望能够使用单个请求编写 3 个文件.

But I want to be able to write lets say 3 files using a single request.

有没有办法在 curl_exec() 之前将字节写入请求?

Is there maybe a way to write bytes to the request before the curl_exec()?

推荐答案

一个完整的例子应该是这样的:

A complete example would look something like this :

<?php
$xml = "some random data";
$post = array(
     "uploadData"=>"@/Users/whowho/test.txt", 
     "randomData"=>$xml, 
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim("http://someURL/someTHing"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);


?>

这篇关于在单个 CURL 请求中写入多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在单个 CURL 请求中写入多个文件

基础教程推荐