我已经尝试过阅读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 :(得分:11)
您可以使用Client::post()
magic method创建HTTP POST请求:
$client = new Client($this->url);
$request = $client->post(
'',
['Content-Type' => 'text/xml; charset=UTF8'],
$xml,
['timeout' => 120]
);
$response = $request->send()->xml();;