在PHP中循环时出现奇怪的API问题
我正在使用此API:https://gatewaydtx1.giact.com/gVerifyV2/POST/Verify.asmx?op=Call在php中使用curl。我可以在对API的单个调用中进行测试。但是,当我尝试循环多个记录时,在第一个记录之后的每次尝试中都会出现错误。在PHP中循环时出现奇怪的API问题
这里是我的代码:
<?
//set the variables for posting
$CompanyID = "123";
$Token = "013443234-224e-4f46-bad4-6693deae2231";
$CheckNumber = "1";
$Amount = "30";
$UniqueID = "111";
$url = "https://gatewaydtx1.giact.com/gVerifyV2/POST/Verify.asmx/Call";
//Get the records from table
$sql = "SELECT id,account_no,routing_no FROM banktable WHERE(status = 'queued') LIMIT 0,100";
$result = mysql_query($sql) or die("Error: " . mysql_error() . "<br>");
while($row = mysql_fetch_array($result)) {
$RoutingNumber = $row['routing_no'];
$AccountNumber = $row['account_no'];
//Do the curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
$post_array = array(
"CompanyID"=>$CompanyID,
"Token"=>$Token,
"RoutingNumber"=>$RoutingNumber,
"AccountNumber"=>$AccountNumber,
"CheckNumber"=>$CheckNumber,
"Amount"=>$Amount,
"UniqueID"=>$UniqueID,
);
//url-ify the data
foreach($post_array as $key=>$value){
$post_array_string .= $key.'='.$value.'&';
}
$post_array_string = rtrim($post_array_string,'&');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST,count($post_array));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_array_string);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
}
?>
这里就是循环4行这个代码后输出:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.giact.com/webservices/gVerifyV2/">33302261|true|No Data|ND00</string>
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
注意,它尝试的第一个纪录产生正确的结果。之后,错误。即使我在这里专门提到了我的循环,但我应该注意到,如果我只是在页面上硬编码两个或更多的卷发,也会发生这种情况。
//url-ify the data
foreach($post_array as $key=>$value){
$post_array_string .= $key.'='.$value.'&';
}
$post_array_string = rtrim($post_array_string,'&');
我想你需要清除每个循环中的$ post_array_string变量。
unset($post_array_string);
PHP的范围并不像C++和许多其他语言。在循环内声明的变量可以在其外部访问。 –
这样做!我是个白痴。非常感谢! –
前:
foreach($post_array as $key=>$value){
地址:
$post_array_string = '';
或者您可以使用http_build_query()
功能。
你是对的,谢谢! –
'// url-ify the data' =>幸运的是,我们有'http_build_query':P其余的它看起来更像是一个数据问题:如果你随机播放你的输入(即跳过第一条记录,直接第二个):它突然成功了,还是失败了,就像它被称为第二个一样? – Wrikken
如果他们解决了您的问题,您应该接受问题的答案。 –