php - 在 PHP 中循环时出现奇怪的 API 问题

标签 php mysql api curl

我在这里使用这个 API:https://gatewaydtx1.giact.com/gVerifyV2/POST/Verify.asmx?op=Call在 php 中使用 curl。我能够在对 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).

请注意,它尝试的第一个记录产生了正确的结果。在那之后,错误。尽管我在这里特别提到了我的循环,但我应该注意,如果我只是在页面上硬编码两个或多个 curl ,也会发生这种情况。

最佳答案

//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 - 在 PHP 中循环时出现奇怪的 API 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092497/

相关文章:

mysql - 在 MySQL 中,如何在字符串值为 'true' 时插入 1,在 false 时插入 0,同时保留空值

php - Laravel 尝试将 HTML 保存在数据库中

iphone - 有没有 API 可以在两个 iphone/ipod Touch/ipad 之间共享数据? (游戏套件除外)

php - 将 jQuery 添加到 Magento

php - 如何使用 php 向 gps 跟踪器发送命令

php - 使用 PHP 组织日期格式

mysql - SQL 在按 column1 分组后更新 column3 并比较 column2 中的值

api - 列出 Jenkins 下的所有文件夹及其路径

javascript - GitHub API使用ajax上传图片

php - 为什么 php 在 die(); 之后继续解释?