Welcome Guest, Not a member yet? Register   Sign In
Error in using CURLRequest Class in case of GET type request
#1

(This post was last modified: 10-12-2022, 01:12 AM by datamweb.)

Hi guys, I'm use CURLRequest Class

I used to have no problem using it, but now I have a problem.
The answer to command

Code:
curl https://api.github.com/user

is as follows:

Code:
{
  "message": "Requires authentication",
  "documentation_url": "https://docs.github.com/rest/reference/users#get-the-authenticated-user"
}

So I expect to see the same response as above when I use:

PHP Code:
           
$client 
= \Config\Services::curlrequest();
                $response $client->request('GET''https://api.github.com/user', [
                    ['debug' => true],
                ]);

            return json_decode($response->getBody()); 

But I have an error, see the content of file log:

Code:
CRITICAL - 2022-10-12 02:03:36 --> 35 : OpenSSL SSL_connect: Connection was reset in connection to api.github.com:443
in SYSTEMPATH\HTTP\CURLRequest.php on line 655.
1 SYSTEMPATH\HTTP\CURLRequest.php(655): CodeIgniter\HTTP\Exceptions\HTTPException::forCurlError('35', 'OpenSSL SSL_connect: Connection was reset in connection to api.github.com:443 ')
2 SYSTEMPATH\HTTP\CURLRequest.php(363): CodeIgniter\HTTP\CURLRequest->sendRequest([...])
3 SYSTEMPATH\HTTP\CURLRequest.php(136): CodeIgniter\HTTP\CURLRequest->send('GET', 'https://api.github.com/user')
4 APPPATH\Controllers\Home.php(36): CodeIgniter\HTTP\CURLRequest->request('GET', 'https://api.github.com/user', [...])
5 SYSTEMPATH\CodeIgniter.php(902): App\Controllers\Home->index()
6 SYSTEMPATH\CodeIgniter.php(457): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Home))
7 SYSTEMPATH\CodeIgniter.php(340): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
8 FCPATH\index.php(67): CodeIgniter\CodeIgniter->run()
9 SYSTEMPATH\Commands\Server\rewrite.php(46): require_once('FCPATH\\index.php')

can you help Where is the problem?
Reply
#2

Thats a GitHub error not CodeIgniter, CodeIgniter is relaying the error.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@InsiteFX Thanks for the reply.

My question is exactly the same, why don't I have such an error in the Terminal or Postman?
It is the same with Google(https://www.googleapis.com/oauth2/v3/userinfo).

And what is the solution?
Reply
#4

I'm not sure what's going on with SSL, but you should get some information from the debug option (in your example, it's set incorrect).
Try something like this:

PHP Code:
$response $client->request('GET''https://api.github.com/user', [
    'headers' => [
        'User-Agent' => 'datamweb/1.0'
    ],
    'http_errors' => false,
    'debug' => true,
]); 

I would not assume to always get JSON in response. Instead, check the content type and status code.
Reply
#5

(This post was last modified: 10-12-2022, 04:35 PM by kenjis.)

Code:
$ curl https://api.github.com/user -i
HTTP/2 401
server: GitHub.com
date: Wed, 12 Oct 2022 23:18:01 GMT
content-type: application/json; charset=utf-8
content-length: 131
x-github-media-type: github.v3; format=json
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
access-control-allow-origin: *
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'
vary: Accept-Encoding, Accept, X-Requested-With
x-ratelimit-limit: 60
x-ratelimit-remaining: 48
x-ratelimit-reset: 1665619900
x-ratelimit-resource: core
x-ratelimit-used: 12
x-github-request-id: EF15:68C0:4A7D3:680E7:63474B37

{"message":"Requires authentication","documentation_url":"https://docs.github.com/rest/reference/users#get-the-authenticated-user"}

Quote:By default, CURLRequest will fail if the HTTP code returned is greater than or equal to 400. You can set http_errors to false to return the content instead:
https://codeigniter4.github.io/CodeIgnit...ttp-errors

PHP Code:
        $client  = \Config\Services::curlrequest();
        $response $client->request(
            'GET',
            'https://api.github.com/user',
            [
                'headers' => [
                    'User-Agent' => 'datamweb/1.0',
                ],
                'http_errors' => false,
            ]
        );

        return $response->getStatusCode()."\n".$response->getBody(); 

I see
Code:
401
{"message":"Requires authentication","documentation_url":"https://docs.github.com/rest/reference/users#get-the-authenticated-user"}

If I removed Usesr-Agent, I got this:
Quote:Request forbidden by administrative rules. Please make sure your request has a User-Agent header (https://docs.github.com/en/rest/overview...t-required). Check https://developer.github.com for other possible causes.

It seems I cannot reproduce the OpenSSL error:
Code:
OpenSSL SSL_connect: Connection was reset in connection to api.github.com:443
Reply
#6

@michalsn And @kenjis .Thank you ,
by correcting the said items, the problem was solved.

Also, error "OpenSSL SSL_connect: Connection was reset in connection to api.github.com:443" is not related to your CI or code, if someone from IRAN has this problem, message me directly.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB