php – 将此cURL转换为Guzzle

前端之家收集整理的这篇文章主要介绍了php – 将此cURL转换为Guzzle前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试过阅读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);
您可以使用 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();;

猜你在找的PHP相关文章