Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Is this working for you? #2

Open
billivycarter opened this issue Nov 23, 2014 · 1 comment
Open

Is this working for you? #2

billivycarter opened this issue Nov 23, 2014 · 1 comment

Comments

@billivycarter
Copy link

Seems incompatible with updated Guzzle... especially with the class names.
Error after error trying to shove a request through shopify.php
Can you add a bit of documentation, or at least a note that this no longer works with recent Laravel and Guzzle versions?


UPDATE:

Got it working by:

  • Adjusting the guzzle namespaces from Guzzle\Http to GuzzleHttp (to match what is in the autoload_classmap.php)
  • Updating the new Client parameters to match what the updated guzzle expects to receive (array, not string)
  • Removing a null parameter in the makeRequest function (expects an array there, not null)
  • Fixing the response mechanism to a valid function

Below is my working code for shopify.php. I have tested with GET requests only so far. Have a feeling POST/PUT/DELETE will take some doing.

public function __construct($domain, $key, $password)
        {
            $url          = "https://" . $key . ":" . $password . "@" . $domain . "/admin/";
            $this->client = new Client([
                'base_url' => $url,
                'defaults' => ['headers' => ['Content-Type' => 'application/json']]
            ]);
        }

        /**
         * send off the request to Shopify, encoding the data as JSON
         *
         * @param  string $method
         * @param  string $page
         * @param  array  $data
         *
         * @return array
         */
        private function makeRequest($method, $page, $data = [])
        {

            $r = $this->client->createRequest($method, $page, $data);

            if ($data && $method != 'GET') {
                $r->setBody(json_encode($data), 'application/json');
            }

            if ($method == 'GET' && !empty($data)) {
            }

            try {
                $response = $this->client->send($r);
                return $response->json();
            } catch (ClientErrorResponseException $e) {
                return [
                    'error'    => $e->getMessage(),
                    'url'      => $e->getRequest()
                                    ->getUrl(),
                    'request'  => $e->getRequest(),
                    'status'   => $e->getResponse()
                                    ->getStatusCode(),
                    'response' => $e->getResponse()
                ];

            } catch (BadResponseException $e) {
                return [
                    'error'    => $e->getMessage(),
                    'url'      => $e->getRequest()
                                    ->getUrl(),
                    'request'  => $e->getRequest(),
                    'status'   => $e->getResponse()
                                    ->getStatusCode(),
                    'response' => $e->getResponse()
                ];
            }

        }

To access in a controller, I use:

    public function index()
    {
        $data = ['query'=>['limit'=>'250']];
        return Shopify::getProducts($data);
    }

Maybe this will help someone down the line—

@aaronflorey
Copy link
Owner

Sorry about that, it sems like i don't get emails from GitHub when someone comments on my repo.

I see you fixed it up in your own fork, did you want to send a pull request?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants