• I have problems getting the Automattic WooCommerce rest api wrapper to work.
    My api calls seem to work and i get the correct response when debugging but i also always get a fatal error with “unexpected charactar”.
    WooCommerce is 2.6 and pretty permalinks are active.

    Guessing it has somthing to do with my PHP configuration?

    I have posted the question on SO as well but no response.
    http://stackoverflow.com/questions/40213457/woocommerce-api-unexpected-character

    My code,

    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    use Automattic\WooCommerce\Client;
    
    $woocommerce = new Client(
        'url',
        'ck_***********',
        'cs_***********',
        [
            'wp_api' => true,
            'version' => 'wc/v1'
        ]
    );
    
    header('Access-Control-Allow-Origin: *');  
    header('Content-type: application/javascript');
    
    use Automattic\WooCommerce\HttpClient\HttpClientException;
    
    try {
        $results = $woocommerce->get('webhooks');
    } catch (HttpClientException $e) {
        \print_r($e);
        \print_r($e->getMessage() . PHP_EOL);
        \print_r('Code: ' . $e->getResponse()->getCode() . PHP_EOL);
        \print_r('Body: ' . $e->getResponse()->getBody() . PHP_EOL);
    }

    The debugg result,

    Automattic\WooCommerce\HttpClient\HttpClientException Object
    (
        [request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request Object
            (
                [url:Automattic\WooCommerce\HttpClient\Request:private] => http://url.local/wp-json/wc/v1/webhooks?
                [method:Automattic\WooCommerce\HttpClient\Request:private] => GET
                [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array
                    (
                        [oauth_consumer_key] => ck_****
                        [oauth_nonce] => *
                        [oauth_signature] => *
                        [oauth_signature_method] => HMAC-SHA256
                        [oauth_timestamp] => *
                    )
    
                [headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
                    (
                        [Accept] => application/json
                        [Content-Type] => application/json
                        [User-Agent] => WooCommerce API Client-PHP/1.1.2
                    )
    
                [body:Automattic\WooCommerce\HttpClient\Request:private] => 
            )
    
        [response:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Response Object
            (
                [code:Automattic\WooCommerce\HttpClient\Response:private] => 200
                [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array
                    (
                        [Date] => Mon, 24 Oct 2016 07:48:59 GMT
                        [Server] => Apache
                        [X-Robots-Tag] => noindex
                        [Link] => <http://url.local/wp-json/>; 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
                        [Expires] => Wed, 11 Jan 1984 05:00:00 GMT
                        [Cache-Control] => no-cache, must-revalidate, max-age=0
                        [Last-Modified] => 
                        [X-WP-Total] => 0
                        [X-WP-TotalPages] => 0
                        [Allow] => GET, POST
                        [Content-Length] => 5
                        [Content-Type] => application/json; charset=UTF-8
                    )
    
                [body:Automattic\WooCommerce\HttpClient\Response:private] => []
            )
    
        [message:protected] => unexpected character
        [string:Exception:private] => 
        [code:protected] => 200
        [file:protected] => /path/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
        [line:protected] => 331
        [trace:Exception:private] => Array
            (
                [0] => Array
                    (
                        [file] => /path/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
                        [line] => 370
                        [function] => processResponse
                        [class] => Automattic\WooCommerce\HttpClient\HttpClient
                        [type] => ->
                        [args] => Array
                            (
                            )
    
                    )
    
                [1] => Array
                    (
                        [file] => /path/vendor/automattic/woocommerce/src/WooCommerce/Client.php
                        [line] => 82
                        [function] => request
                        [class] => Automattic\WooCommerce\HttpClient\HttpClient
                        [type] => ->
                        [args] => Array
                            (
                                [0] => webhooks
                                [1] => GET
                                [2] => Array
                                    (
                                    )
    
                                [3] => Array
                                    (
                                    )
    
                            )
    
                    )
    
                [2] => Array
                    (
                        [file] => /path/test.php
                        [line] => 23
                        [function] => get
                        [class] => Automattic\WooCommerce\Client
                        [type] => ->
                        [args] => Array
                            (
                                [0] => webhooks
                            )
    
                    )
    
            )
    
        [previous:Exception:private] => 
    )
    unexpected character
    Code: 200
    Body: []

The topic ‘Automattic WooCommerce rest API wrapper’ is closed to new replies.