将此 cURL 转换为 Guzzle

2024-02-25

我尝试阅读 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://docs.guzzlephp.org/en/stable/quickstart.html#sending-requests创建 HTTP POST 请求:

$client = new Client($this->url);
$request = $client->post(
    '', 
    ['Content-Type' => 'text/xml; charset=utf-8'], 
    $xml, 
    ['timeout' => 120]
);
$response = $request->send()->xml();;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将此 cURL 转换为 Guzzle 的相关文章

随机推荐