不正确的响应 json 编码和数组违规 Woocomerce 3

Incorrect response json coding and array violation Woocomerce 3

我得到的答案编码错误。也许正因为如此,数组构建不起作用。如何解决?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?get_status");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);

$decoded = (array) json_decode($result);

// Update order status
if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['status']) && ! empty($decoded['status']) ){
    if( $decoded['status'] == "Order Completed" )
        $order->update_status( 'completed' );
    elseif( $decoded['status'] == "Order Cancelled" )
        $order->update_status( 'cancelled' );
}

我找了解决方法试了,也没用:

$decoded = (array) json_decode($result, JSON_UNESCAPED_UNICODE);

print_r( $decoded ); 显示:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

print_r ( $result ); 显示:

HTTP/1.1 200 OK Server: nginx/1.2.1 Date: Thu, 05 Jul 2018 18:59:07 GMT Content-Type: text/html; charset=windows-1251 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.4.45-0+deb7u2 Vary: Accept-Encoding Strict-Transport-Security: max-age=31536000; {"result":"success","status":"\u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d"}

您要求在服务器响应中接收 header 信息。您无法使用 json_decode().

对其进行解码

要从服务器获得纯 JSON-only 响应,您需要设置:

curl_setopt($ch, CURLOPT_HEADER, false);

一旦在响应中获得有效的 JSON,就可以将其转换为 PHP 数组,如下所示(将第二个参数设置为 true):$decoded = json_decode($result, true);

现在$decoded是一个可以遍历的PHP关联数组。