简体   繁体   中英

Converting this cURL for Guzzle

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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