2011-04-06 3 views
5

Existe-t-il un moyen d'utiliser PHP curl pour envoyer plusieurs fichiers dans une seule requête?Écrire plusieurs fichiers dans une seule requête CURL

Je comprends que vous pouvez envoyer un fichier unique en utilisant des éléments suivants:

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

Mais je veux être en mesure d'écrire permet de dire 3 fichiers en utilisant une seule requête.

Y at-il peut-être un moyen d'écrire des octets à la demande avant le curl_exec()?

Répondre

8

Un exemple complet ressemblerait à quelque chose comme ceci:

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


?> 
+1

Cela ne fonctionnera pas si vous utilisez PHP 5.5+. Vous devez faire cela à la place: http://stackoverflow.com/a/29122849/112192 – Jaffer

5

Vous pouvez utiliser cette

$post = array(
    "file1"=>"@/path/to/myfile1.jpg", 
    "file2"=>"@/path/to/myfile2.jpg", 
); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
+0

a fait l'affaire. Merci. – Koekiebox

Questions connexes