我有这种类型的卷曲声明
curl -u xxx:yyy -d“aaa = 111”http://someapi.com/someservice
我想从列表中运行这个变化的aaa = bbb
更新:基于吉米代码构建的代码
<?PHP
$data = array('Aaa', 'Bbb', 'Ccc');
echo $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug
curl_setopt($ch, CURLOPT_USERPWD, "xxx:yyy");
foreach ($data as $param) {
curl_setopt($ch, CURLOPT_URL, 'http://...?aaa='.$param);
$response = curl_exec($ch);
echo "<hr>".$response;
}
?>
答案 0 :(得分:2)
在PHP中,代码看起来会有所不同,因为您可能想要使用built-in cURL client而不是exec()
(编辑为包含循环):
$data = array('111', '222', 'ccc');
foreach ($data as $param)
{
$ch = curl_init();
$params = array('aaa' => $param);
curl_setopt($ch, CURLOPT_URL, 'http://someapi.com/someservice');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "xxx:yyy");
/* used for POST
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
*/
// for GET, we just override the URL
curl_setopt($ch, CURLOPT_URL, 'http://someapi.com/someservice?aaa='.$param);
$response = curl_exec($ch);
}
答案 1 :(得分:2)
在Perl中,您可能会使用LWP
,因为它包含在标准发行版中。
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
my $host = 'localhost'; # 'someapi.com';
my $url = "http://$host/someservice";
my $ua = LWP::UserAgent->new( keep_alive => 1 );
$ua->credentials( $host, 'your realm', 'fridolin', 's3cr3t' );
for my $val ( '111', '222', '333' ) {
my $req = POST $url, [aaa => $val];
$req->dump; # debug output
my $rsp = $ua->request( $req );
if ( $rsp->is_success ) {
print $rsp->decoded_content;
} else {
print STDERR $rsp->status_line, "\n";
}
}