Do a GET Request with Guzzle 6.0 and PHP 7

artgrphartgrph Partner Posts: 10 partner

Hi guys. Am having a hard time getting a response from lightspeed using php an Guzzle. Am sure someone out there has already done this, all I get is 401 Unauthorized error. I make sure to refresh my token and that the access token is valid already. This is the code I wrote to set up the request:


    $this->guzzle_client = new \GuzzleHttp\Client( [

                                        'base_uri' => 'https://cloud.merchantos.com:443',

                    //'port' => 443,

                    //'timeout' => 5.0

                    ]);



//later in the code

        $request = $this->guzzle_client->request("GET", $uri,

[

                       'headers' => [

                        'Authorization' => "Bearer $this->access_token",

                        'Accept' => 'application/json'

                        ]

                       ]

                       );



Here is the debug output from Guzzle:


* Trying 104.16.116.93...* TCP_NODELAY set* Connected to cloud.merchantos.com (104.16.116.93) port 443 (#0)* ALPN, offering http/1.1* successfully set certificate verify locations:* CAfile: /etc/ssl/cert.pem CApath: none* SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305* ALPN, server accepted to use http/1.1* Server certificate:* subject: C=US; ST=CA; L=San Francisco; O=CloudFlare, Inc.; CN=merchantos.com* start date: Mar 27 00:00:00 2019 GMT* expire date: Mar 27 12:00:00 2020 GMT* subjectAltName: host "cloud.merchantos.com" matched cert's "*.merchantos.com"* issuer: C=US; ST=CA; L=San Francisco; O=CloudFlare, Inc.; CN=CloudFlare Inc ECC CA-2* SSL certificate verify ok.> GET /API/Account/127785/Customer/?load_relations=all&firstName=Rafael&lastName=Sabino HTTP/1.1 Host: cloud.merchantos.com User-Agent: GuzzleHttp/6.3.3 curl/7.64.1 PHP/7.3.8 < HTTP/1.1 401 Unauthorized < Date: Wed, 16 Oct 2019 01:29:01 GMT < Content-Type: application/vnd.merchantos.pos-v1+xml < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d727cf3b5ff530af56122d55d4600efea1571189341; expires=Thu, 15-Oct-20 01:29:01 GMT; path=/; domain=.merchantos.com; HttpOnly; Secure < x-frame-options: SAMEORIGIN < X-XSS-Protection: 1; mode=block < X-Content-Type-Options: nosniff < WWW-Authenticate: Basic realm="MerchantOS API" < X-LS-Master-System: false < X-LS-Master-Catalog: false < X-LS-Master-Account: false < Vary: Accept-Encoding < CF-Cache-Status: DYNAMIC < Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"

< Server: cloudflare < CF-RAY: 526642693a4f93fa-SJC



If I'm missing anything, let me know. Thanks!

2 comments

  • artgrphartgrph Partner Posts: 10 partner

    Here is code that does the refreshing of the tokens, it works just fine it seems, no errors:


            $data = [

                  'grant_type' => 'refresh_token',

                  'client_id' => $clientID,

                  'client_secret' => 'my-fake-secret',

                  'refresh_token' => $code,

        

                 ];

            $user->lastRefreshTime = time();

            $connection = new \GuzzleHttp\Client( [

                                            'base_uri' => 'https://cloud.merchantos.com',

                        'port' => 443

                        ]);


            $result = $connection->request("POST", "/oauth/access_token.php", ['form_params' => $data]);

            $result = $result->getBody()->getContents();


            $result = (array)json_decode($result);

  • artgrphartgrph Partner Posts: 10 partner

    I was using the library incorrectly, one correct way of setting up the client is to set it up this way:


        $this->clover_connection = new \GuzzleHttp\Client( [

                                            'base_uri' => 'https://us.merchantos.com:443',

                         'headers' => 

     [

                          'Authorization' => "Bearer $this->access_token",

                          'Accept' => 'application/json'

     ]

                        ]);


    This will cause the header options to actually be included in the request, which wasn't happening before.

Sign In or Register to comment.