简体   繁体   English

将此cURL转换为Guzzle

[英]Converting this cURL for Guzzle

I've tried reading through the Guzzle docs but I can't wrap my head around this problem. 我试过阅读Guzzle文档,但我无法解决这个问题。

I want to use Guzzle instead of cURL for the following: 我想使用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);

I tried this but I'm still new at this...: 我试过这个,但我还是新来的......:

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

You can make use of the Client::post() magic method to create a HTTP POST request: 您可以使用Client::post()魔术方法来创建HTTP POST请求:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM