将该cURL转换为Guzzle [英] Converting this cURL for Guzzle

查看:103
本文介绍了将该cURL转换为Guzzle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读Guzzle文档,但无法解决这个问题.

I've tried reading through the Guzzle docs but I can't wrap my head around this problem.

对于以下情况,我想使用Guzzle而不是cURL:

I want to use Guzzle instead of cURL for the following:

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);

推荐答案

您可以使用 Client::post()魔术方法创建HTTP POST请求:

You can make use of the Client::post() magic method to create a HTTP POST request:

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

这篇关于将该cURL转换为Guzzle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆