Skip to content

Commit

Permalink
Fix curl problem with server certificate verification
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed May 9, 2020
1 parent 5ad4280 commit a1acf82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Services/Requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Inspirum\Balikobot\Exceptions\BadRequestException;
use Inspirum\Balikobot\Exceptions\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;

class Requester implements RequesterInterface
{
Expand Down Expand Up @@ -101,6 +102,8 @@ public function request(string $url, array $data = []): ResponseInterface
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// set data
if (count($data) > 0) {
Expand All @@ -115,13 +118,18 @@ public function request(string $url, array $data = []): ResponseInterface
]);

// execute curl
$response = (string) curl_exec($ch);
$statusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// check for errors.
if ($response === false) {
throw new RuntimeException(curl_error($ch), curl_errno($ch));
}

// close curl
curl_close($ch);

return new Response($statusCode, [], $response);
return new Response((int) $statusCode, [], (string) $response);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/RequesterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Inspirum\Balikobot\Tests\Integration\Balikobot;

use Inspirum\Balikobot\Services\Requester;
use Inspirum\Balikobot\Tests\AbstractTestCase;
use RuntimeException;

class RequesterTest extends AbstractTestCase
{
public function testThrowsErrorOnRequestError()
{
$this->expectException(RuntimeException::class);

$requester = new Requester('test', 'test');

$requester->request('dummy');
}
}

0 comments on commit a1acf82

Please sign in to comment.