doumao1519 2014-12-15 12:06
浏览 125
已采纳

使用cert将Curl请求转换为Guzzle问题

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.

  • 写回答

1条回答 默认 最新

  • dou70260 2014-12-15 19:58
    关注

    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 !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历