将带有列表文件的数组传递给curl循环 [英] pass array with list file to curl loop

查看:88
本文介绍了将带有列表文件的数组传递给curl循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否可以将满足文件链接要求的数组传递给curl循环以创建单个循环,而不是每次需要上传文件内容时,都不要复制和粘贴相同的curl调用.不同的文件名.

i have a doubt about if i can pass an array that content the link of the files to a curl loop to create a single loops and not everytime i need to upload a file content, copy and paste the same curl call with the different file name.

这是我编写的将单个文件上传到服务器的代码.我为每个文件复制并粘贴了相同的代码,这样,一切正常,但是我想了解如何创建一个单独的循环,以赋予"$ data = array('file'=> $ fn);".调用函数"file_get_contents($ fn)"时,使用一个函数循环所有文件内容并自动执行所有调用.

Here are the code i wrote to upload into the server a single file. I copy and paste the same code for every single file and in that way everything works, but i want to understand how to create a single loop giving to the "$data = array('file'=>$fn);" the loop with all the file content and automate all the calls in one function when call the function "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包含一个带有文件列表的数组.如果我想调用创建的但仅用于特定file1.rdf的updateContent()是正确的,则可以使用该命令updateContent($files['0']);调用仅为元素0指定的方法(对应于所提到的数组的file1.rdf)进行更新它的状态?

Hi, once again i have a doubt. If i want to use that script as a function to call its method into an another script that read one by one the articles, here i have to put the function updateContent(){} to call into the other script the method updateContent()? As i already said, $fn contain an array with the list of the file. If i want to call the updateContent() created but only for a specific file1.rdf is correct to use that command updateContent($files['0']); to call the method specified only for the element 0 (correspond to file1.rdf of the array mentioned) to update it status?

推荐答案

您的意思是这样的吗?

$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 />";
    }
}

这篇关于将带有列表文件的数组传递给curl循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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