【问题标题】:Strange API issue when looping in PHP在 PHP 中循环时出现奇怪的 API 问题
【发布时间】:2012-06-20 23:49:03
【问题描述】:

我在这里使用这个 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).

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

【问题讨论】:

  • //url-ify the data => 幸运的是,我们有 http_build_query :P 其余的对我来说更像是一个数据问题:如果你随机输入你的输入(即跳过第一个记录,直接去对于第二个):然后它会突然成功,还是像被称为第二个一样失败?
  • 如果您的问题解决了您的问题,您应该接受他们的答案。

标签: php mysql api curl


【解决方案1】:
//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++ 和许多其他语言那么严格。在循环内声明的变量可以在循环外访问。
【解决方案2】:

之前:

foreach($post_array as $key=&gt;$value){

添加:

$post_array_string = '';

或者你可以使用http_build_query()函数。

【讨论】:

    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多