2016-06-21 241 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()); 
    } 
}