2013-10-22 7 views
8

Guzzle 문서를 읽었지만이 문제를 해결할 수 없습니다.Guzzle에 대한이 cURL 변환

나는 다음과 같은 대신 컬 목구멍 사용하려면 :

protected $url = 'https://secure.abcdef.com/cgi/xml_request_server.php'; 

    $xml = "<ABCRequest>\n"; 
    $xml .=  "<Authentication>\n"; 
    $xml .=   "<ABLogin>$this->gwlogin</ABLogin>\n"; 
    $xml .=   "<ABKey>$this->gwkey</ABKey>\n"; 
    $xml .=  "</Authentication>\n"; 
    $xml .=  "<Request>\n"; 
    $xml .=   "<RequestType>SearchABC</RequestType>\n"; 
    $xml .=  "</Request>\n"; 
    $xml .= "</ABCRequest>\n"; 

    $header = "POST $this->url HTTP/1.1\n"; 
    $header .= "Host: domain.com\n"; 
    $header .= "Content-Length: ".strlen($xml)."\n"; 
    $header .= "Content-Type: text/xml; charset=UTF8\n"; 
    $header .= "Connection: close; Keep-Alive\n\n"; 
    $header .= $xml; 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $this->url); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); 

    $response = curl_exec($ch); 
    curl_close($ch); 

나는이 시도하지만이 여전히 새로운 해요 ... :

$client = new Client($this->url); 
$response = $client->get($xml); 
+0

당신이 이미 가지고 대답뿐만 아니라 당신에 대한 자세한 내용은 유용 할 수 있습니다 Client::post()는 HTTP POST 요청을 만들려면 PHP의 HEREDOC 유형 문자열 : http://php.net/language.types.string.php#language.types.string.syntax.heredoc – hakre

답변

11

당신은 사용할 수있다

$client = new Client($this->url); 
$request = $client->post(
    '', 
    ['Content-Type' => 'text/xml; charset=UTF8'], 
    $xml, 
    ['timeout' => 120] 
); 
$response = $request->send()->xml();; 
+1

Perfect. xml : $ request = $ this-> client-> post ('', array(), $ xml)를 얻기 위해 약간 수정했습니다. \t \t $ response-> send() -> xml(); – Dru

+0

이 예제는 120 초의 제한 시간 설정을 다루지 않으며 요청 본문 내에서 콘텐츠 형식 및 charset 인코딩 사용을 다루지 않는다고 생각합니다. – hakre

+0

@hakre 업데이트를 시도하고 문서를 읽으십시오. http://guzzlephp.org/http-client/client.html#creating-requests-with-a-client 여기 샘플입니다. –