解析来自curl_exec调用的响应POST数据

发布于 2024-11-28 17:50:56 字数 1270 浏览 3 评论 0 原文

我有以下代码块,它连接到安全服务(支付交易网关),传入一些字段($postData)并接收响应($returnValue)。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://secure.service.com/data');
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if ($returnValue = curl_exec($ch))
{
    $error = curl_error($ch);
}

当我显示 $returnValue ...的内容时,它们显示如下:

HTTP/1.0 200 OK 批准日期:2011 年 8 月 10 日星期三 09:24:15 GMT 连接:关闭内容类型:application/x-www-form-urlencoded 内容长度:182 avs_code=X&cvv2_code=P&status_code=1&处理器=TEST&auth_code=999999&settle_amount=2000& ;settle_currency=美元&trans_id=120741127516&auth_msg=TEST+APPROVED&auth_date=2011-08-10+09:24:15

是否有方法或 CURL 调用可以将此结果字符串分解为其组成部分?或者这是我需要自己写的东西?我需要获取响应代码 (200)、批准/拒绝部分 (Approved) 和查询字符串 (avs_code ....)。我尝试查看curl_getinfo,但这只是让我获得HTTP响应代码,而不是批准/拒绝或查询字符串值。

我是 PHP 新手,因此如果我缺少明显的方法调用或 CURL 参数,请告诉我。

I have the following code block which connects out to a secure service (payment transaction gateway), passes in some fields ($postData) and receives a response ($returnValue).

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://secure.service.com/data');
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if ($returnValue = curl_exec($ch))
{
    $error = curl_error($ch);
}

When I display the contents of $returnValue ... they show as follows:

HTTP/1.0 200 OK Approved Date: Wed, 10 Aug 2011 09:24:15 GMT
Connection: close Content-Type: application/x-www-form-urlencoded
Content-Length: 182
avs_code=X&cvv2_code=P&status_code=1&processor=TEST&auth_code=999999&settle_amount=2000&settle_currency=USD&trans_id=120741127516&auth_msg=TEST+APPROVED&auth_date=2011-08-10+09:24:15

Is there a method or CURL call that breaks apart this result string into its component parts? or is that something I need to write myself? I need to get the response code (200), the approval/decline part (Approved) and the query string (avs_code ....). I tried looking through the curl_getinfo but that is only getting me the HTTP Response code, not the approval/decline or the query string values.

I'm a novice at PHP so please let me know if I'm missing an obvious method call or CURL parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-12-05 17:50:56
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIE, "");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    $header=substr($result,0,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
    $body=substr($result,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
    curl_close($ch);

比使用类似 preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);

参见 preg_match_all 手册

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIE, "");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    $header=substr($result,0,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
    $body=substr($result,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
    curl_close($ch);

Than use something like preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);

See preg_match_all manual

墨小墨 2024-12-05 17:50:56

我建议你看看 http://php.net/manual/en/ function.http-parse-headers.php 手册,如果您不想安装 PECL pecl_http - 有有用的用户评论。例如:

function http_parse_headers($header) {
    $retVal = array();
    $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
    foreach ($fields as $field) {
        if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
            $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
            if (isset($retVal[$match[1]])) {
                $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
            } else {
                $retVal[$match[1]] = trim($match[2]);
            }
        }
    }
    return $retVal;
}

您可以使用 http://php 解析 cookie 并发布值。 net/manual/ru/function.http-parse-cookie.php (或注释中的函数)和 http://php.net/manual/en/function.parse-str.php 。 HTTP协议是相当透明的。

I suggest you look at http://php.net/manual/en/function.http-parse-headers.php manual, if you don't want to install PECL pecl_http - there are usefull user comments. For example:

function http_parse_headers($header) {
    $retVal = array();
    $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
    foreach ($fields as $field) {
        if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
            $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
            if (isset($retVal[$match[1]])) {
                $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
            } else {
                $retVal[$match[1]] = trim($match[2]);
            }
        }
    }
    return $retVal;
}

You can parse cookies and post values with http://php.net/manual/ru/function.http-parse-cookie.php (or functions in comments) and http://php.net/manual/en/function.parse-str.php . HTTP protocol is rather transparent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文