我想将Woocommerce与Rest API一起使用。
所以,我的过程:
第三步适用于此: https://packagist.org/packages/automattic/woocommerce
但是,我无法获得数据,如产品。
我的代码:
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://delphes.local',
'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840',
'cs_479ce18567c180263f130072dea082b52872e6e9',
[
'wp_api' => true,
'version' => 'wc/v1',
'verify_ssl' => false,
]
);
var_dump($woocommerce);
use Automattic\WooCommerce\HttpClient\HttpClientException;
try {
$results = $woocommerce->get('products');
// 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).
echo "ok";
var_dump($results);
var_dump($lastRequest);
var_dump($lastResponse);
}catch (HttpClientException $e) {
var_dump($e->getMessage()); // Error message.
var_dump($e->getRequest()); // Last request data.
var_dump($e->getResponse()); // Last response data.
}
回复:
object(Automattic\WooCommerce\Client)[2906]
public 'http' =>
object(Automattic\WooCommerce\HttpClient\HttpClient)[2907]
protected 'ch' => null
protected 'url' => string 'http://delphes.local/wp-json/wc/v1/' (length=35)
protected 'consumerKey' => string 'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840' (length=43)
protected 'consumerSecret' => string 'cs_479ce18567c180263f130072dea082b52872e6e9' (length=43)
protected 'options' =>
object(Automattic\WooCommerce\HttpClient\Options)[2908]
private 'options' =>
array (size=3)
...
private 'request' => null
private 'response' => null
private 'responseHeaders' => null
string 'cURL Error: Operation timed out after 15008 milliseconds with 0 bytes received' (length=78)
object(Automattic\WooCommerce\HttpClient\Request)[2909]
private 'url' => string 'http://delphes.local/wp-json/wc/v1/products?oauth_consumer_key=ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840&oauth_nonce=fbfd6ee91b9586fa47816b924c79c1c4b1bf3d5c&oauth_signature=PXhShXEu0bgVQuo8pNkLKREUvGcwUyCl593osEF0Akk%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1475658731' (length=284)
private 'method' => string 'GET' (length=3)
private 'parameters' =>
array (size=5)
'oauth_consumer_key' => string 'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840' (length=43)
'oauth_nonce' => string 'fbfd6ee91b9586fa47816b924c79c1c4b1bf3d5c' (length=40)
'oauth_signature' => string 'PXhShXEu0bgVQuo8pNkLKREUvGcwUyCl593osEF0Akk=' (length=44)
'oauth_signature_method' => string 'HMAC-SHA256' (length=11)
'oauth_timestamp' => int 1475658731
private 'headers' =>
array (size=3)
'Accept' => string 'application/json' (length=16)
'Content-Type' => string 'application/json' (length=16)
'User-Agent' => string 'WooCommerce API Client-PHP/1.1.1' (length=32)
private 'body' => string '' (length=0)
object(Automattic\WooCommerce\HttpClient\Response)[2911]
private 'code' => int 0
private 'headers' =>
array (size=0)
empty
private 'body' => boolean false
最后我对此有同样的回应:
警告:无法修改标头信息 - 已在D:\ delphes \ wp-includes \ rest-api中发送的标头(输出从D:\ delphes \ wp-content \ themes \ delphes \ functions.php:49开始)第1196行的\ class-wp-rest-server.php
和
{"代码":" woocommerce_rest_cannot_view","消息":"抱歉,您无法列出资源","数据& #34; {"状态":401}}
我也在node.js中尝试过:woocommerce-api (npm package)
使用这个包,我不能通过require(通过import或require)初始化类。 我有这个错误:
未捕获错误:无法找到模块" net"
=>在cookies.js中:32来自包" hard-cookie"。
安装此缺失的软件包后,出现了新的错误:
未捕获错误:找不到模块" ../ package.json"
cookies中的:38 ..........
所以,我已经放弃了这种方式。
现在,我和Automattic住在一起。
也许,我的vhost
或我的htaccess
会出现问题?
我在搜索这个问题之前已经搜索了很长时间。
您如何看待这个error 401
?
为一个好的社区度过美好的一天:)