我有以下代码块,它连接到安全服务(支付交易网关),传入一些字段($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.
发布评论
评论(2)
比使用类似
preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);
参见 preg_match_all 手册
Than use something like
preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);
See preg_match_all manual
我建议你看看 http://php.net/manual/en/ function.http-parse-headers.php 手册,如果您不想安装 PECL pecl_http - 有有用的用户评论。例如:
您可以使用 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:
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.