• Resolved tomzorz

    (@tomzorz)


    Hi, I’m trying to set up a connection with the WooCommerce 2.6 Rest api through the official php library.

    Here’s my code:

    require __DIR__ . '/vendor/autoload.php';
    
    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;
    
    echo "<pre>";
    
    // WC
    $woocommerce = new Client(
        'https://aromacorner.be/dev/wp', 
        'xxx', 
        'xxx',
        [
            'wp_api' => true,
            'version' => 'wc/v1',
        ]
    );
    
    try {
        // Array of response results.
        $results = $woocommerce->get('orders');
        print_r($results);
        // Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
    
        // Last request data.
        $lastRequest = $woocommerce->http->getRequest();
        $lastRequest->getUrl(); // Requested URL (string).
        $lastRequest->getMethod(); // Request method (string).
        $lastRequest->getParameters(); // Request parameters (array).
        $lastRequest->getHeaders(); // Request headers (array).
        $lastRequest->getBody(); // Request body (JSON).
    
        // Last response data.
        $lastResponse = $woocommerce->http->getResponse();
        $lastResponse->getCode(); // Response code (int).
        $lastResponse->getHeaders(); // Response headers (array).
        $lastResponse->getBody(); // Response body (JSON).
    
    } catch (HttpClientException $e) {
        $e->getMessage(); // Error message.
        $e->getRequest(); // Last request data.
        $e->getResponse(); // Last response data.
        print_r( $e);
    }

    And here’s the output:

    Automattic\WooCommerce\HttpClient\HttpClientException Object
    (
        [request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request Object
            (
                [url:Automattic\WooCommerce\HttpClient\Request:private] => https://aromacorner.be/dev/wp/wp-json/wc/v1/orders
                [method:Automattic\WooCommerce\HttpClient\Request:private] => GET
                [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array
                    (
                    )
    
                [headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
                    (
                        [Accept] => application/json
                        [Content-Type] => application/json
                        [User-Agent] => WooCommerce API Client-PHP/1.1.1
                    )
    
                [body:Automattic\WooCommerce\HttpClient\Request:private] => 
            )
    
        [response:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Response Object
            (
                [code:Automattic\WooCommerce\HttpClient\Response:private] => 401
                [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array
                    (
                        [Date] => Mon, 03 Oct 2016 20:45:16 GMT
                        [Server] => Apache
                        [Pragma] => no-cache
                        [Link] => ; rel="https://api.w.org/"
                        [X-Content-Type-Options] => nosniff
                        [Access-Control-Expose-Headers] => X-WP-Total, X-WP-TotalPages
                        [Access-Control-Allow-Headers] => Authorization
                        [X-Robots-Tag] => noindex
                        [Expires] => Wed, 11 Jan 1984 05:00:00 GMT
                        [Cache-Control] => no-cache, must-revalidate, max-age=0
                        [Set-Cookie] => PHPSESSID=fdkcjmnhtd59sjona6f9no9pi5; path=/
                        [Vary] => User-Agent
                        [Transfer-Encoding] => chunked
                        [Content-Type] => text/html; charset=utf-8
                    )
    
                [body:Automattic\WooCommerce\HttpClient\Response:private] => 
    
    	
    	
    	
    	
    	
    
    	
    
    Unauthorized
    
            )
    
        [message:protected] => Syntax error
        [string:Exception:private] => 
        [code:protected] => 401
        [file:protected] => /Users/tom/Sites/aromacorner/scripts_etc/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
        [line:protected] => 331
        [trace:Exception:private] => Array
            (
                [0] => Array
                    (
                        [file] => /Users/tom/Sites/aromacorner/scripts_etc/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
                        [line] => 370
                        [function] => processResponse
                        [class] => Automattic\WooCommerce\HttpClient\HttpClient
                        [type] => ->
                        [args] => Array
                            (
                            )
    
                    )
    
                [1] => Array
                    (
                        [file] => /Users/tom/Sites/aromacorner/scripts_etc/vendor/automattic/woocommerce/src/WooCommerce/Client.php
                        [line] => 82
                        [function] => request
                        [class] => Automattic\WooCommerce\HttpClient\HttpClient
                        [type] => ->
                        [args] => Array
                            (
                                [0] => orders
                                [1] => GET
                                [2] => Array
                                    (
                                    )
    
                                [3] => Array
                                    (
                                    )
    
                            )
    
                    )
    
                [2] => Array
                    (
                        [file] => /Users/tom/Sites/aromacorner/scripts_etc/check_sc.php
                        [line] => 44
                        [function] => get
                        [class] => Automattic\WooCommerce\Client
                        [type] => ->
                        [args] => Array
                            (
                                [0] => orders
                            )
    
                    )
    
            )
    
        [previous:Exception:private] => 
    )

    I’m running this through my mamp installation.
    Anyone who can point out the cause of this?

    Thanks in advance!

    • This topic was modified 8 years, 1 month ago by tomzorz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tomzorz

    (@tomzorz)

    Meanwhile, I have been able to make a successful GET request through a simple HTTP client with the following url: GET https://www.aromacorner.be/dev/wp/wp-json/wc/v1/orders/5600?consumer_key=ck_xxx&consumer_secret=cs_xxx

    I changed the url to contain ‘www’, wich did the trick with this simple get request, but doesn’t change anything when trying in with the given php code.
    So why would that work with the http client and getting it through the php library give an Unauthorized error?

    • This reply was modified 8 years, 1 month ago by tomzorz.
    Thread Starter tomzorz

    (@tomzorz)

    Although I don’t quite understand perfectly why, but adding 'query_string_auth' => true as an option solved it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WC 2.6 Rest api error’ is closed to new replies.