2016-06-21 247 views
2

我正在使用WooCommerce-REST-API-Client-Library-v2并试图进行简单的获取订单调用。我没有问题通过http建立到我的测试服务器的连接,但是当我尝试使用https连接到生产服务器时,我在响应中收到无效的JSON返回错误。在woocommerce设置中启用安全结帐。我尝试过使用和不使用base64_encoding凭证。有人可以告诉我我做错了什么,或者提供一个正确格式的示例,使用WooCommerce-REST-API-Client-Library-v2通过HTTPS进行身份验证。这是我的代码。谢谢!如何使用HTTPS验证Woocommerce API(基本身份验证)

$consumer_key = base64_encode('ck_removed'); // Add your own Consumer Key here 
$consumer_secret = base64_encode('cs_removed'); // Add your own Consumer Secret here 
$store_url = 'https://removed.co.uk'; // Add the home URL to the store you want to connect to here 
$is_ssl= TRUE; 

try { 
    $client = new WC_API_Client($consumer_key, $consumer_secret, $store_url); 
    // Get WC Orders Request 
    $orders=$client->orders->get(); 
    $orders=$orders['orders']; 
} catch (WC_API_Client_Exception $e) { 
    echo $e->getMessage() . PHP_EOL; 
    echo $e->getCode() . PHP_EOL; 
    if ($e instanceof WC_API_Client_HTTP_Exception) { 
     print_r($e->get_request()); 
     print_r($e->get_response()); 
    } 
} 

回答

1

如果存在HTTP状态码:301永久移动,则您试图访问的端点不支持SSL/TLS。因此,将主机从HTTPS更改为HTTP,即可完成。但是,如果端点启用了SSL/TLS,则可以使用基于密钥(公钥,私钥)的基本身份验证。在支持SSL/TLS的端点上,OAuth不起作用。祝你好运!

$options = array(
    'debug'   => true, 
    'return_as_array' => false, 
    'validate_url' => false, 
    'timeout'   => 30, 
    'ssl_verify'  => false 
); 

try { 
    $client = new WC_API_Client($http_store_url, $consumer_key $consumer_secret, $options); 
    $orders=$client->orders->get(); 
    print_r($orders); 
} catch (WC_API_Client_Exception $e) { 
    echo $e->getMessage() . PHP_EOL; 
    echo $e->getCode() . PHP_EOL; 
    if ($e instanceof WC_API_Client_HTTP_Exception) { 
     print_r($e->get_request()); 
     print_r($e->get_response()); 
    } 
}