简体   繁体   中英

Converting Curl request to Guzzle issues with cert

I am Trying to convert an existing phpCurl request to latest guzzle and not getting anywhere fast.

This is the current request.

        $curl_opts = array(
        CURLOPT_HEADER => false,
        CURLOPT_HTTPHEADER => array('Content-Type: text/json', 'Content-length: '.strlen($json)),
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $json,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => 'https://the-domainservice.php',
        CURLOPT_VERBOSE => false,
        CURLOPT_SSLCERT => '/path/to/file.pem',
        CURLOPT_SSLCERTTYPE => 'pem',
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1
    );

    $curl = curl_init();
    curl_setopt_array($curl, $curl_opts);

    $response = curl_exec($curl);

For Guzzle I have tried so many ways but here are a couple of example.

        $response = $this->client->post('https://the-domainservice.php', [
            'body' => $postData,
            'cert' => '/path/to/file.pem',
            'config' => [
                'curl' => [
                    CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_RETURNTRANSFER => true
                ]
            ]
        ]
    );

And more verbose of

        $request = $this->client->createRequest('POST', 'https://the-domainservice.php', [
        'cert' => '/path/to/file.pem',
        'verify' => false,
        'headers' => [
            'Content-Type' => 'text/json',
            'Content-length' => strlen(json_encode($postData))
        ]
    ]);

    $postBody = $request->getBody();

    foreach ($postData as $key => $value) {
        $postBody->setField($key, $value);
    }

    $response = $this->client->send($request);

For my guzzle requests I am just getting

GuzzleHttp\\Exception\\ServerException: Server error response [url] https://the-domainservice.php [status code] 500 [reason phrase] Internal Service Error

Really hope someone can advise.

Feel a little stupid now.

Just needed to send json data and all worked, end result was.

$response = $this->client->post('https://the-domainservice.php', [
        'body' => json_encode($postData),
        'cert' => '/path/to/file.pem',
    ]
);

Excellent stuff Guzzle !

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