2011-08-10 3 views
0

보안 서비스 (지불 트랜잭션 게이트웨이)에 연결하고 일부 필드 ($ postData)를 전달하고 응답 ($ returnValue)을 수신하는 다음 코드 블록이 있습니다. 다음과 같이curl_exec 호출에서 POST 데이터 응답을 구문 분석

$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년 (수) 그리니치 표준시 09시 24분 15초 연결 8월 10일 닫기 콘텐츠 유형 : 애플리케이션/x-www-form-urlencoded를 콘텐츠 길이 : 182 avs_code = X = P cvv2_code & & & STATUS_CODE = 1 개 프로세서 TEST = & AUTH_CODE = 999999 & & settle_amount = 2,000 settle_ 통화 = USD & trans_id = 120741127516 & auth_msg = TEST + 승인 & auth_date = + 09 2011-08-10 : 24 :

15인가가 그 구성 부분으로 떨어져이 결과 문자열을 분해하는 방법이나 CURL 호출 ? 아니면 나 자신을 쓸 필요가있는 것이지? 응답 코드 (200), 승인/거부 부분 (승인) 및 쿼리 문자열 (avs_code ....)을 가져와야합니다. 나는 curl_getinfo를 조사해 보았습니다. 그러나 그것은 HTTP 응답 코드가 아니라 승인/거부 또는 쿼리 문자열 값을 얻는 것입니다.

저는 PHP에서 초보자입니다. 따라서 확실한 메서드 호출이나 CURL 매개 변수가 누락 된 경우 알려주십시오.

답변

5
$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); 

난 당신이 PECL의 pecl_http를 설치하지 않을 경우, http://php.net/manual/en/function.http-parse-headers.php에 설명서를 보면 제안 preg_match_all manual

+1

나는 그 substr split 후에 그 vars들 중 하나의 http 응답 spec에 따라 그것들을 분리하는 \ r \ n \ r \ n을 여전히 가질 것이라고 생각한다. 이런 식으로 할 때, 아마 그것들을 제거하고 싶을 것입니다. –

1

를 참조 preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$header,$res);

같은 것을 사용보다 - 유용한 사용자 의견이 있습니다. 예를 들어 :

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.net/manual/ru/function.http-parse-cookie.php와 (또는 함수 주석) 및 http://php.net/manual/en/function.parse-str.php을 구문 분석 할 수 있습니다. HTTP 프로토콜은 다소 투명합니다.