dongxi8297 2013-04-11 05:51
浏览 108

如何在返回标头时解析curl响应

So what I am trying to do, is parse every "node" in the response for lack of better words. It should echo the "tag" followed by a ": " then the value. What I have tried is in the "Code" section. What gets returned from the current code is in the "Response" section. I'm trying to learn from others code and this is where the "Display headers / body / response for debugging and certification purposes" code comes from. I'm just trying to modify it to echo the parsed rawResponse when it clearly does not.

Response:

    headers:
    body:
    rawResponse:AC700000005332001VI4788250000028291sBn551664C1A088C3242DF9CD5C92D52A8FA9EDD532D10100 N097857Approved00N013731
    OrderID: sBn5
    TxRefNum: 51664C1A088C3242DF9CD5C92D52A8FA9EDD532D
    RespCode: 00
    AVSRespCode: 
    CVV2RespCode: N
    AuthCode: 097857

Code:

<?php

$xml_data = "<Request><NewOrder>";
$xml_data .= "<IndustryType>EC</IndustryType>";
$xml_data .= "<MessageType>AC</MessageType>";
$xml_data .= "<BIN>000002</BIN>";
$xml_data .= "<AccountNum>".$varCC."</AccountNum>";
$xml_data .= "<Exp>".$varExpiry."</Exp>";
$xml_data .= "<CardSecVal>".$varCVV."</CardSecVal>";
$xml_data .= "<OrderID>sBn5</OrderID>";
$xml_data .= "<Amount>".$varAmt."</Amount>";
$xml_data .= "</NewOrder></Request>";   

$URL = "https://orbitalvar1.paymentech.net";

// Build header as array for cURL option
$header = "POST /AUTHORIZE HTTP/1.0
";
$header.= "MIME-Version: 1.0
";
$header.= "Content-type: application/PTI46
";
$header.= "Content-length: ".strlen($xml_data)."
";
$header.= "Content-transfer-encoding: text
";
$header.= "Request-number: 1
";
$header.= "Document-type: Request
";
$header.= "Interface-Version: Test 1.4
";
$header.= "Connection: close 

";              
$header.= $xml_data;   

// Define cURL options, then connect to server while saving response
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);

curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$rawResponse = curl_exec($ch);             

if (curl_errno($ch)) {
   print curl_error($ch);
} else {
   curl_close($ch);
}


/////////////////////////////////////////////////////////////////////////////
// Display headers / body / response for debugging and certification purposes
/////////////////////////////////////////////////////////////////////////////
echo "headers:<BR>
";
foreach($header as $value) echo "&nbsp;&nbsp;&nbsp;$value<BR>
";
echo "<BR>body:<BR>
&nbsp;&nbsp;&nbsp;$body<BR><BR><HR>
";
echo "<BR>rawResponse:<BR>
&nbsp;&nbsp;&nbsp;$rawResponse<BR><BR>
";

// Parse rawResponse XML to response array, then only display important values
$parser = xml_parser_create('UTF-8');
xml_parse_into_struct($parser, $rawResponse, $response);
//var_dump($response);
foreach($response as $value){
    switch($value["tag"]){
        case "ORDERID": echo "OrderID: ".$value["value"]."<BR>
"; break;
        case "RESPCODE": echo "RespCode: ".$value["value"]."<BR>
"; break;
        case "AVSRESPCODE": echo "AVSRespCode: ".$value["value"]."<BR>
"; break;
        case "CVV2RESPCODE": echo "CVV2RespCode: ".$value["value"]."<BR>
"; break;
        case "TXREFNUM": echo "TxRefNum: ".$value["value"]."<BR>
"; break;
        case "AUTHCODE": echo "AuthCode: ".$value["value"]."<BR>
"; break;
    }
}unset($value);
?>
  • 写回答

1条回答 默认 最新

  • donglu2008 2013-04-11 06:08
    关注

    You neeed separate http headers from http body. It is a " " snting. So you can find it like this:

    $p = strpos($rawResponse, "
    
    ");
    if( $p !== false ) {
        $rawHeades = substr($rawResponse, 0, $p);
        $rawBody = substr($rawResponse, $p + 4);
    }
    

    and you need some thing after print curl_error($ch); - in this case there isn't any true data in $rawResponse

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器