编辑:我解决了这个问题,下面是代码,以防将来对某人有所帮助:
foreach($array as $value) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$value);
curl_setopt($ch, CURLOPT_USERPWD, $apiUser . ":" . $apiKey);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
$fileResponse = "responses/".( $_FILES['uploadedfile']['name']);
$current = file_get_contents($fileResponse);
$current .= $result ."\r\n";
file_put_contents($fileResponse, $current);
$responseHeader = curl_getinfo($ch);
curl_close($ch);
unset($ch);
}
我一直在挖掘网站,发现了一些类似的问题,但没有一个是正确的。我是一个初学者,所以这可能是一个简单的修复。
我正在尝试创建一个curl脚本,它将使用数组值循环遍历许多http请求。我将使用客户数据来填充csv文件,使用此数据创建http请求,然后使用此脚本通过API发送每个请求并获取响应。我打算使用每组数组元素值作为curl postfields,数组结构类似于以下内容:
[0]=>
array(8) {
string(4) "bln1"
["bfn"]=>
string(4) "bfn1"
["bsn"]=>
string(4) "bsn1"
["bs"]=>
string(3) "bs1"
["bc"]=>
string(3) "bc1"
["bco"]=>
string(4) "bco1"
["tea"]=>
string(4) "tea1"
["man"]=>
string(4) "man1"
}
[1]=>
array(8) {
["bln"]=>
string(4) "bln2"
["bfn"]=>
string(4) "bfn2"
["bsn"]=>
string(4) "bsn2"
["bs"]=>
string(3) "bs2"
["bc"]=>
string(3) "bc2"
["bco"]=>
string(4) "bco2"
["tea"]=>
string(4) "tea2"
["man"]=>
string(4) "man2"
}
[2]=>
array(8) {
["bln"]=>
string(4) "bln3"
["bfn"]=>
string(4) "bfn3"
["bsn"]=>
string(4) "bsn3"
["bs"]=>
string(3) "bs3"
["bc"]=>
string(0) ""
["bco"]=>
string(4) "bco3"
["tea"]=>
string(4) "tea3"
["man"]=>
string(4) "man3"
}
我有一个工作的curl请求循环指定的数字(loops =#)但我无法完成这项工作。我想避免curl_multi并连续发送这些因为我想以相同的顺序捕获请求和响应。
在研究中pass array with list file to curl loop和PHP - Read every line in a CSV, take the data and Post/CURL each line one by one似乎与我想要完成的内容非常相似,但我无法让脚本运行。
非常感谢任何帮助或建议!
编辑:包含和编辑代码。
对于循环,我尝试了两种方法:
$count = count($data);
for ($i = 0; $i < $count; $i++) {
和
foreach($data as $value) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$value);
curl_setopt($ch, CURLOPT_USERPWD, $apiUser . ":" . $apiKey);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
$responseHeader = curl_getinfo($ch);
curl_close($ch);
unset($ch);
}
该数组采用JSON编码,如下所示:
[{"bln":"bln1","bfn":"bfn1","bsn":"bsn1","bs":"bs1","bc":"bc1","bco":"bco1","tea":"tea1","man":"man1"},{"bln":"bln2","bfn":"bfn2","bsn":"bsn2","bs":"bs2","bc":"bc2","bco":"bco2","tea":"tea2","man":"man2"},{"bln":"bln3","bfn":"bfn3","bsn":"bsn3","bs":"bs3","bc":"","bco":"bco3","tea":"tea3","man":"man3"}]