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 !

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

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?