将cURL请求转换为Guzzle PHP

将cURL请求转换为Guzzle PHP,php,curl,guzzle,guzzle6,Php,Curl,Guzzle,Guzzle6,我正在尝试将现有的卷曲请求转换为Guzzle 6。这是cURL请求。curl请求的代码如下所示 $xml = file_get_content('new.xml'); // Initialize handle and set options $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $this->xml_post_url );// curl_setopt( $ch, CURLOPT_SSLCERT, $this->

我正在尝试将现有的卷曲请求转换为Guzzle 6。这是cURL请求。curl请求的代码如下所示

$xml = file_get_content('new.xml');

// Initialize handle and set options
$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL,     $this->xml_post_url );//
curl_setopt( $ch, CURLOPT_SSLCERT, $this->sslcert );//
curl_setopt( $ch, CURLOPT_SSLKEY,  $this->sslkey );//
curl_setopt( $ch, CURLOPT_POSTFIELDS, $raw_request );
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Connection: close') );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt( $ch, CURLOPT_SSLCERTPASSWD, $this->sslcertpasswd );
curl_setopt( $ch, CURLOPT_SSLKEYPASSWD,  $this->sslkeypasswd );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // capture the response to a string

// give it 3 tries
for ($i=1; $i<=3; ++$i) {

    $raw_response = curl_exec($ch);
    $response_HTTP_CODE = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );

    if (200 <= $response_HTTP_CODE and $response_HTTP_CODE < 400) {

        if ($raw_response !== false) {

            curl_close( $ch );
            return $raw_response;

        }

    }
    elseif (500 <= $response_HTTP_CODE and strlen( $raw_response ) > 0) {

        $offset = strpos( $raw_response, '<errortext>');

        if ($offset !== false) {

            curl_close( $ch );
            break;

        }
    }

    sleep( 1 ); // give it a moment
}

curl_close( $ch );

但我遇到了旋度异常。

好的,我终于解决了这个问题。我发布我的解决方案,希望它能帮助别人

$client = new Client();
$client->setDefaultOption('verify', false);

$response = $client->post($endPointUrl, [
                'body'    => ['file_filed' => $xml],
                'cookies' => true,
                'cert'    => [$sslCert, $sslCertPassword],
                'ssl_key' => [$sslKey, $sslKeyPassword]
            ]);

if (200 === $response->getStatusCode()) {

    $xmlResponse = $response->xml();

} else {

    // do something with error.

}

对于重试,我使用了

向我们展示您尝试将此转换为GUZLE的情况。请用代码编辑您的问题,请不要将其放在注释
中,但我遇到了cURL异常。
-什么异常?
$client = new Client();
$client->setDefaultOption('verify', false);

$response = $client->post($endPointUrl, [
                'body'    => ['file_filed' => $xml],
                'cookies' => true,
                'cert'    => [$sslCert, $sslCertPassword],
                'ssl_key' => [$sslKey, $sslKeyPassword]
            ]);

if (200 === $response->getStatusCode()) {

    $xmlResponse = $response->xml();

} else {

    // do something with error.

}