【问题标题】:Converting Curl request to Guzzle issues with cert使用证书将 Curl 请求转换为 Guzzle 问题
【发布时间】:2015-02-13 12:42:39
【问题描述】:

我正在尝试将现有的 phpCurl 请求转换为最新的 guzzle 并且无法快速获得任何地方。

这是当前请求。

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

对于 Guzzle,我尝试了很多方法,但这里有几个例子。

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

还有更详细的

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

对于我的狂热请求,我只是得到了

GuzzleHttp\Exception\ServerException: 服务器错误响应 [url] https://the-domainservice.php [状态码] 500 [原因短语] 内部服务错误

真的希望有人可以建议。

【问题讨论】:

    标签: php curl guzzle


    【解决方案1】:

    现在感觉有点傻。

    只需要发送 json 数据并且一切正常,最终结果是。

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

    优秀的东西 Guzzle !

    【讨论】:

    • 更简单,你可以做'json' => $postData
    猜你喜欢
    • 2017-01-13
    • 2019-12-17
    • 2015-11-02
    • 2017-04-04
    • 2014-09-17
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多