@@ -24,8 +24,9 @@ class QencodeApiClient
2424
2525 public $ url = 'https://api.qencode.com/ ' ;
2626 public $ version = 'v1 ' ;
27+ private $ supported_versions = array ('v1 ' , 'v1.1 ' );
2728
28- const USER_AGENT = 'Qencode PHP API SDK 1.0 ' ;
29+ const USER_AGENT = 'Qencode PHP API SDK 1.1 ' ;
2930
3031 /**
3132 * Maximum amount of time in seconds that is allowed to make the connection to the API server
@@ -42,9 +43,10 @@ class QencodeApiClient
4243 /**
4344 * @param string $key Qencode Project API key
4445 * @param string $url Optional url to any different API endpoint
46+ * @param string $version Optional API version
4547 * @throws \Qencode\Exceptions\QencodeException if the library failed to initialize
4648 */
47- public function __construct ($ key , $ url = null )
49+ public function __construct ($ key , $ url = null , $ version = null )
4850 {
4951 if (strlen ($ key ) < 12 ) {
5052 throw new QencodeException ('Missing or invalid Qencode project api key! ' );
@@ -55,10 +57,25 @@ public function __construct($key, $url = null)
5557 }
5658 $ this ->url = $ url ;
5759 }
60+ if ($ version ) {
61+ $ version = strtolower ($ version );
62+ if (in_array ($ version , $ this ->supported_versions )) {
63+ $ this ->version = $ version ;
64+ if ($ version == 'v1.1 ' ) {
65+ $ this ->url = $ this ->v1_1_get_endpoint ();
66+ }
67+ }
68+ else throw new QencodeException ('Unsupported API version: ' .$ version );
69+ }
5870 $ this ->key = $ key ;
5971 $ this ->getAccessToken ();
6072 }
6173
74+ private function v1_1_get_endpoint () {
75+ $ api_host = file_get_contents ($ this ->url .'/v1.1 ' );
76+ return 'https:// ' .$ api_host ;
77+ }
78+
6279 private function getAccessToken () {
6380 $ response = $ this ->post ("access_token " , array ('api_key ' => $ this ->key ));
6481 $ this ->access_token = $ response ['token ' ];
@@ -127,7 +144,7 @@ private function request($method, $path, array $params = [])
127144 else {
128145 $ url = $ this ->url . '/ ' . $ this ->version . '/ ' . trim ($ path , '/ ' );
129146 }
130-
147+ echo " URL: " . $ url . "\n" ;
131148 if (!empty ($ params ) & is_array ($ params )) {
132149 $ params = http_build_query ($ params );
133150 }
0 commit comments