woocommerce / wc-api-php

WooCommerce REST API PHP Library
https://packagist.org/packages/automattic/woocommerce
MIT License
528 stars 160 forks source link

Syntax error #212

Closed ChrisDubois36 closed 5 years ago

ChrisDubois36 commented 6 years ago

Hello, im pretty newbie working on API REST and i have an issue when making a simple get request on products with Syntax Error like this :

Syntax error

Automattic\WooCommerce\HttpClient\Request Object ( [url:Automattic\WooCommerce\HttpClient\Request:private] => http://www.##################/wp-json/wc/v2/products?oauth_consumer_key=ck_0ab286671a8160f7c493a634e712ff25c6379c6e&oauth_nonce=d6446d84fb68ec8dbd87323aedbb16d097f37951&oauth_signature=V22EQsKAe7u0G1CCI1YCQD0waiN93Wzbk4Mp%2BxPHi9U%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1531473955 [method:Automattic\WooCommerce\HttpClient\Request:private] => GET [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array ( [oauth_consumer_key] => ck_0ab286671a8160f7c493a634e712ff25c6379c6e [oauth_nonce] => d6446d84fb68ec8dbd87323aedbb16d097f37951 [oauth_signature] => V22EQsKAe7u0G1CCI1YCQD0waiN93Wzbk4Mp+xPHi9U= [oauth_signature_method] => HMAC-SHA256 [oauth_timestamp] => 1531473955 ) [headers:Automattic\WooCommerce\HttpClient\Request:private] => Array ( [Accept] => application/json [User-Agent] => WooCommerce API Client-PHP/2.0.1 ) [body:Automattic\WooCommerce\HttpClient\Request:private] => ) 

Automattic\WooCommerce\HttpClient\Response Object ( [code:Automattic\WooCommerce\HttpClient\Response:private] => 302 [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array ( [Connection] => close [Pragma] => no-cache [cache-control] => no-cache [Location] => /WVOZZ/wp-json/wc/v2/products?oauth_consumer_key=ck_0ab286671a8160f7c493a634e712ff25c6379c6e&oauth_nonce=d6446d84fb68ec8dbd87323aedbb16d097f37951&oauth_signature=V22EQsKAe7u0G1CCI1YCQD0waiN93Wzbk4Mp%2BxPHi9U%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1531473955 ) [body:Automattic\WooCommerce\HttpClient\Response:private] => )

Here is my code for the request, please help me i don't know where is the problem

require 'vendor/autoload.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/BasicAuth.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClientException.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/OAuth.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/Options.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/Request.php';
require 'vendor/automattic/woocommerce/src/WooCommerce/HttpClient/Response.php';

require 'vendor/automattic/woocommerce/src/WooCommerce/Client.php';

use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\BasicAuth;
use Automattic\WooCommerce\HttpClient\HttpClientException;
use Automattic\WooCommerce\HttpClient\OAuth;
use Automattic\WooCommerce\HttpClient\Options;
use Automattic\WooCommerce\HttpClient\Request;
use Automattic\WooCommerce\HttpClient\Response;

$woocommerce = new Client(
    'http://www.#########.fr', 
    'ck_###############', 
    'cs_###############',
    [
        'wp_api' => true,
        'version' => 'wc/v2',
    ]
);
$endpoint = "products";

try {
    // Array of response results.
    $results = $woocommerce->get($endpoint);
    echo "$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) {
    $err = $e->getMessage(); // Error message.
    $req = $e->getRequest(); // Last request data.
    $res = $e->getResponse(); // Last response data.
}
print_r($err);
echo '<br><br>';
print_r($req);
echo '<br><br>';
print_r($res);
hippoduck commented 6 years ago

Did you work out the error? I'm getting the exact same issue, the error message isn't very helpful.

ecuci commented 6 years ago

Any progress about this error?

metju90 commented 6 years ago

This is a very depressing and generic error. I am getting this problem when I use a specific endpoint. However, when I use an experimental endpoint the API is working fine.

hippoduck commented 6 years ago

I did work it out in the end, something to do with API settings on WordPress. I think it was how Wordpreess was returning the results or something.

I actually need to work on this again tomorrow to setup another WooCommerce customer on our platform so I will figure it out again and let you guys know.

meincms commented 6 years ago

Any Updates on the issue? I do get the same error. I am not using the localhost, but an own development domain on mamp pro. but I do work with the https-protocol.

hippoduck commented 6 years ago

I think all I did to fix this issue was to change the permalinks option to "Post Name" within WordPress. In fact, this is crucial otherwise it won't work as per the WooCommerce Api documentation here: https://woocommerce.github.io/woocommerce-rest-api-docs/v3.html#version

You can test this by going to (after logging into WP-Admin) https://wordpressite.com/wp-json/wp/v2/posts If you get an error, then your permalinks are not setup correctly.

  1. Log into WP-Admin
  2. Go to Settings > Permalinks
  3. Select "Post name"
  4. Save changes.

Also, under "WooCommerce" > "Settings" > "Advanced" > "Legacy API", I have this enabled.

claudiosanches commented 5 years ago

this error comes from your server, there is a syntax error in the JSON response. This could be generated by your theme, plugins or custom code, also as reported, maybe permalinks needing to be updated again for some bug, still this is not an issue with this REST API library, so I'm closing.

JavierLob commented 5 years ago

Print de body response on HttpClient:processResponse() on line 366, print that result and see what's the response of the request. In my case was a garbage text of notice and warning because I have the debug mode active. I hope that helps someone with that trick to find the error.