2013-10-22 6 views
8

Guzzleのドキュメントを読んでみましたが、この問題を回避することはできません。GuzzleのこのcURLの変換

私は次のための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

あなたが既に持っている答えに加えて、 PHPのHEREDOCタイプの文字列:http://php.net/language.types.string.php#language.types.string.syntax.heredoc – hakre

答えて

11

あなたが利用することができますClient::post()を使用してHTTP POSTリクエストを作成してください:

+1

パーフェクト。私はxml:$ request = $ this-> client-> post( ''、array()、$ xml)を得るために少し修正しました。 \t \t $ response = $ request-> send() - > xml(); – Dru

+0

この例では、タイムアウトの設定が120秒であるとは考えていませんが、要求本体内のコンテンツタイプとcharsetエンコーディングの使用については言及していません。 – hakre

+0

@hakre更新してみてください。docsを読んでください - http://guzzlephp.org/http-client/client.html#creating-requests-with-a-clientここにサンプルです –

関連する問題