通过阵列列表文件卷曲环(pass array with list file to curl loop

2019-10-29 05:11发布

我有一个关于我是否可以传递一个数组内容的文件到卷曲循环的链接,创建一个单一的循环,而不是每次我需要上传一个文件的内容,复制和粘贴用不同的文件名相同的卷曲通话疑问。

下面是我写上传到服务器的单个文件的代码。 我复制和粘贴为每一个文件,并以这种方式一切工作相同的代码,但我想知道如何创建一个循环给人以“$数据=阵列(‘文件’=> $ FN);” 与所有的文件内容循环和一个函数调用函数“的file_get_contents(FN $)”时,自动完成所有的来电。

//file to upload

$fn = ("fn.rdf");

//curl loop to upload single file
    $data = array('file'=>$fn);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/rdf+xml'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/index.php?update=true');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($fn));  
    curl_exec($ch);
    $info = curl_getinfo($ch); var_dump($info);
    $result = curl_exec($ch); 
     if ($result === false) {
       die(curl_error());
     }else{
       echo "<br>"."upload ok"."<br>";
     }

谢谢!

编辑:你好,我再一次有一个疑问。 如果我想使用该脚本作为一个函数来调用它的方法到另一个脚本,通过一个文章,在这里,我必须把读一个function updateContent(){}调入其他脚本方法updateContent() 正如我已经说过,$ FN包含与文件列表中的数组。 如果我要拨打的updateContent()创建,但只针对特定的file1.rdf正确使用该命令updateContent($files['0']); 调用只为元素0中指定的方法(对应于所提及的阵列的file1.rdf)来更新它的状态?

Answer 1:

你的意思是这样呢?

$files = array('1.rdf', '2.rdf', '3.rdf', 'x.rdf', 'whatever.xyz');

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/rdf+xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/index.php?update=true');
curl_setopt($ch, CURLOPT_POST, true);

foreach ($files as $fn) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($fn));  
    $result = curl_exec($ch);
    $info = curl_getinfo($ch); 
    var_dump($info);
    if ($result === false) {
       die(curl_error());
    }else{
       echo "<br />".$fn." upload ok"."<br />";
    }
}


文章来源: pass array with list file to curl loop