json - WC API:返回无效的 JSON
问题描述
我在这里做错了什么?!自从我杀死 Google 和 stackoverflow 已经两天了......我想不通:(((
我已经在线检查了返回的正文 json(由于它很长,所以没有将其粘贴在这里)并且是有效的 json ......问题出在哪里?!
没有安装缓存插件,邮递员中的相同请求返回OK结果,直接在浏览器中测试请求URL返回OK结果,一切都很好,除非我在我的项目中使用......
Automattic\WooCommerce\HttpClient\HttpClientException 对象([request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request 对象([url:Automattic\WooCommerce\HttpClient\Request:private] => https: //www.viatainrawz.ro/wp-json/wc/v3/products [方法:Automattic\WooCommerce\HttpClient\Request:private] => GET [参数:Automattic\WooCommerce\HttpClient\Request:private] => 数组 ( )
[headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
(
[Accept] => application/json
[Content-Type] => application/json
[User-Agent] => WooCommerce API Client-PHP/1.0.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, 31 Aug 2020 09:26:42 GMT
[content-type] => application/json; charset=UTF-8
[vary] => Accept-Encoding
[x-robots-tag] => noindex
[x-content-type-options] => nosniff
[access-control-expose-headers] => X-WP-Total, X-WP-TotalPages, Link
[access-control-allow-headers] => Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
[expires] => Wed, 11 Jan 1984 05:00:00 GMT
[cache-control] => no-cache, must-revalidate, max-age=0
[x-wp-total] => 49
[x-wp-totalpages] => 5
[link] => <https://viatainrawz.ro/wp-json/wc/v3/products?page=2>; rel="next"
[allow] => GET, POST
[server] => ClausWeb-nginx
[cluster-host] => server46.romania-webhosting.com
)
)
[message:protected] => Invalid JSON returned
[string:Exception:private] =>
[code:protected] => 200
[file:protected] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
[line:protected] => 303
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
[line] => 346
[function] => lookForErrors
[class] => Automattic\WooCommerce\HttpClient\HttpClient
[type] => ->
[args] => Array
(
[0] =>
)
)
[1] => Array
(
[file] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
[line] => 382
[function] => processResponse
[class] => Automattic\WooCommerce\HttpClient\HttpClient
[type] => ->
[args] => Array
(
)
)
[2] => Array
(
[file] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/Client.php
[line] => 82
[function] => request
[class] => Automattic\WooCommerce\HttpClient\HttpClient
[type] => ->
[args] => Array
(
[0] => products
[1] => GET
[2] => Array
(
)
[3] => Array
(
)
)
)
[3] => Array
(
[file] => /home/vreaumob/worker.vreaumobila.ro/apitest.php
[line] => 24
[function] => get
[class] => Automattic\WooCommerce\Client
[type] => ->
[args] => Array
(
[0] => products
)
)
)
[previous:Exception:private] =>
) 返回无效的 JSON
解决方案
我使用的代码:
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
try {
$woocommerce = new Client( 'https://www.viatainrawz.ro','XX', 'XX',
[
'verify_ssl' => false,
'version' => 'wc/v3'
]
);
print_r($woocommerce->get('products'));
} 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);
}
推荐阅读
- java - JBOSS/Wildfly 21 org.jboss.msc.service.StartException 在服务 jboss.persistenceunit
- java - WebLogic 10.3.6 升级到 12c,从 JSF 1.1 迁移到 1.2 会导致错误,特别是在尝试查找 JSF 1.2 库时
- excel - 打开工作簿并在两个工作簿中复制具有可变范围的数据
- jquery - 使用 jQuery Ajax 加载和覆盖当前页面 url 和内容
- python - 如何根据列中的数值创建逗号分隔的数字列表
- swiftui - 如何在 SwiftUI 的一个视图中缩放和排列多个 UIBezierPath?
- sql-server - 用于更改登录属性并禁用“强制密码过期”的 SQLCMD 语法
- azure-devops - 在 azure devops 部署期间覆盖 helm 图表中的配置映射
- javascript - 如何在没有确认按钮的情况下提交 Hcaptcha。(2验证码/数据库)
- oracle - 如何覆盖 Oracle Apex 以上传文件