|
| 1 | +# ScientiaMobile WURFL Cloud Client for PHP |
| 2 | + |
| 3 | +The WURFL Cloud Service by ScientiaMobile, Inc., is a cloud-based |
| 4 | +mobile device detection service that can quickly and accurately |
| 5 | +detect over 500 capabilities of visiting devices. It can differentiate |
| 6 | +between portable mobile devices, desktop devices, SmartTVs and any |
| 7 | +other types of devices that have a web browser. |
| 8 | + |
| 9 | +This is the PHP Client for accessing the WURFL Cloud Service, and |
| 10 | +it requires a free or paid WURFL Cloud account from ScientiaMobile: |
| 11 | +http://www.scientiamobile.com/cloud |
| 12 | + |
| 13 | +## Installation |
| 14 | +-------------- |
| 15 | +### Requirements |
| 16 | + |
| 17 | + - `PHP 5.4+` |
| 18 | + - `json` extension (almost always included) |
| 19 | + - `curl` extension is recommended |
| 20 | + |
| 21 | +### Sign up for WURFL Cloud |
| 22 | +First, you must go to http://www.scientiamobile.com/cloud and signup |
| 23 | +for a free or paid WURFL Cloud account (see above). When you've finished |
| 24 | +creating your account, and have selected the WURFL Capabilities that you |
| 25 | +would like to use, you must copy your API Key, as it will be needed in |
| 26 | +the Client. |
| 27 | + |
| 28 | +### Via Composer |
| 29 | + |
| 30 | + composer require scientiamobile/wurflcloud |
| 31 | + |
| 32 | +### Via Source |
| 33 | + |
| 34 | +[Download the source code](https://github.com/WURFL/wurfl-cloud-client-php/zipball/master) |
| 35 | + |
| 36 | + require_once '/path/to/cloud/client/src/autoload.php'; |
| 37 | + |
| 38 | +### Example WURFL Cloud Client |
| 39 | +From your web browser, you should go to the WURFL Cloud Client's |
| 40 | +examples/ folder. You will see the Compatibility Test Script, |
| 41 | +which will verify that your configuration is compatible with |
| 42 | +the WURFL Cloud Client. |
| 43 | + |
| 44 | +You should test your API Key from this page by pasting it in the |
| 45 | +input box, then clicking "Test API Key". If successful, you will |
| 46 | +see "Your server is able to access WURFL Cloud and your API Key was |
| 47 | +accepted." If there was a problem, the error message will be |
| 48 | +displayed instead. Please note that it may take a few minutes from |
| 49 | +the time that you signup for your WURFL Cloud API Key to become active. |
| 50 | + |
| 51 | +### Integration |
| 52 | +You should review the included examples (`example.php`, `MyWurfl.php`, |
| 53 | +`show_capabilities.php`) to get a feel for the Client API, and how |
| 54 | +best to use it in your application. |
| 55 | + |
| 56 | +Here's a quick example of how to get up and running quickly: |
| 57 | + |
| 58 | +```php |
| 59 | +// Include the autoloader - edit this path! |
| 60 | +require_once '../src/autoload.php'; |
| 61 | +// Create a configuration object |
| 62 | +$config = new ScientiaMobile\WurflCloud\Config(); |
| 63 | +// Set your WURFL Cloud API Key |
| 64 | +$config->api_key = 'xxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; |
| 65 | +// Create the WURFL Cloud Client |
| 66 | +$client = new ScientiaMobile\WurflCloud\Client($config); |
| 67 | +// Detect your device |
| 68 | +$client->detectDevice(); |
| 69 | +// Use the capabilities |
| 70 | +if ($client->getDeviceCapability('is_wireless_device')) { |
| 71 | + echo "This is a mobile device"; |
| 72 | +} else { |
| 73 | + echo "This is a desktop device"; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Upgrading from v1.x |
| 78 | +----------- |
| 79 | +**IMPORTANT**: Since version 2.0.0, the WURFL Cloud Client for PHP uses |
| 80 | +namespaces. In addition, things were moved around a bit to allow |
| 81 | +for easier configuration. |
| 82 | + |
| 83 | +To upgrade your existing v1 code, you will need to replace calls to |
| 84 | +the following classes: |
| 85 | + |
| 86 | + old: |
| 87 | + require_once '../Client/Client.php'; |
| 88 | + WurflCloud_Client_Config |
| 89 | + WurflCloud_Client_Client |
| 90 | + WurflCloud_Cache_Null |
| 91 | + WurflCloud_Cache_Cookie |
| 92 | + |
| 93 | + new: |
| 94 | + require_once '../src/autoload.php'; |
| 95 | + ScientiaMobile\WurflCloud\Config |
| 96 | + ScientiaMobile\WurflCloud\Client |
| 97 | + ScientiaMobile\WurflCloud\Cache\Null |
| 98 | + ScientiaMobile\WurflCloud\Cache\Cookie |
| 99 | + |
| 100 | +Every other class was also renamed/namespaced, so if you are using them |
| 101 | +directly, you can follow the mapping above to figure out the new name. |
| 102 | + |
| 103 | +If you were using the `MyWurfl.php` singleton, you can use the new one |
| 104 | +and no code changes will be required in your application. |
| 105 | + |
| 106 | +Note that in v2, support for proxy servers was added as well as better |
| 107 | +support for timeouts. More on these options is found below. |
| 108 | + |
| 109 | +## Configuration |
| 110 | +--------------- |
| 111 | +The WURFL Cloud Client object `ScientiaMobile\WurflCloud\Client` takes three |
| 112 | +arguments: `Config`, `Cache`, `HttpClient`. |
| 113 | + |
| 114 | +### Config |
| 115 | +`ScientiaMobile\WurflCloud\Config` |
| 116 | +Used for setting the WURFL Cloud API Key `api_key` and adding |
| 117 | +`addCloudServer()` / removing `clearServers()` WURFL Cloud Servers. |
| 118 | + |
| 119 | +### Cache (optional) |
| 120 | +Standard caching classes: |
| 121 | + |
| 122 | + - `ScientiaMobile\WurflCloud\Cache\Null`: Disables caching completely |
| 123 | + - `ScientiaMobile\WurflCloud\Cache\Cookie`: Cookie-based caching |
| 124 | + |
| 125 | +**Premium WURFL Cloud accounts** have access to additional, high-performance |
| 126 | +caching layers. Note that these files are only included in the WURFL Cloud |
| 127 | +Client package downloaded from a Premium account. Please refer to the |
| 128 | +included examples for specific cache adapter configuration. |
| 129 | + |
| 130 | + - `ScientiaMobile\WurflCloud\Cache\File`: Filesystem-based caching |
| 131 | + - `ScientiaMobile\WurflCloud\Cache\APC`: APC memory-based caching |
| 132 | + - `ScientiaMobile\WurflCloud\Cache\Memcache`: Memcached distributed memory-based |
| 133 | + caching using the PHP `memcache` extension |
| 134 | + - `ScientiaMobile\WurflCloud\Cache\Memcached`: Memcached distributed memory-based |
| 135 | + caching using the PHP `memcached` extension |
| 136 | + |
| 137 | +### HttpClient (optional) |
| 138 | + - `ScientiaMobile\WurflCloud\HttpClient\Fsock`: Uses native PHP `fsock` calls |
| 139 | + - `ScientiaMobile\WurflCloud\HttpClient\Curl`: Uses the PHP extension `curl` |
| 140 | + - `ScientiaMobile\WurflCloud\HttpClient\Guzzle`: Uses the [Guzzle HTTP client](http://guzzlephp.org/) |
| 141 | + |
| 142 | +Note: to use `Guzzle`, you must have the Guzzle library loaded. To load it locally, |
| 143 | +you can run the following command from the root of the WURFL Cloud Client folder: |
| 144 | + |
| 145 | + composer update |
| 146 | + |
| 147 | +Then make sure to use the composer autoloader `vendor/autoload.php` instead of the |
| 148 | +built in one (`src/sutoload.php`). |
| 149 | + |
| 150 | +#### Proxy Server Configuration |
| 151 | +The `Curl` and `Guzzle` HTTP Clients support a proxy server configuration via the |
| 152 | +`setProxy()` method: |
| 153 | + |
| 154 | +```php |
| 155 | +// Common proxy examples |
| 156 | +$http_client = new ScientiaMobile\WurflCloud\HttpClient\Curl(); |
| 157 | + |
| 158 | +// Socks 4 Proxy |
| 159 | +$http_client->setProxy("socks4://192.168.1.1:1080"); |
| 160 | + |
| 161 | +// Socks 4a Proxy |
| 162 | +$http_client->setProxy("socks4a://192.168.1.1:1080"); |
| 163 | + |
| 164 | +// Socks 5 Proxy |
| 165 | +$http_client->setProxy("socks5://192.168.1.1:1080"); |
| 166 | + |
| 167 | +// Socks 5 Proxy + Authentication |
| 168 | +$http_client->setProxy("socks5://someuser:somepass@192.168.1.1:1080"); |
| 169 | + |
| 170 | +// HTTP Proxy |
| 171 | +$http_client->setProxy("http://192.168.1.1:8080"); |
| 172 | + |
| 173 | +// HTTP Proxy + Authentication |
| 174 | +$http_client->setProxy("http://someuser:somepass@192.168.1.1:8080"); |
| 175 | + |
| 176 | +// Pass $http_client in to the Client to use it |
| 177 | +$client = new ScientiaMobile\WurflCloud\Client($config, $cache, $http_client); |
| 178 | +``` |
| 179 | + |
| 180 | +#### HTTP Timeout |
| 181 | +The WURFL Cloud Client is set to forcefully terminate the WURFL Cloud request |
| 182 | +after 1 second if a response has not been received. In some cases you may want |
| 183 | +to increase this timeout to account for high latency. All of the HTTP Clients |
| 184 | +support the `setTimeout()` method: |
| 185 | + |
| 186 | +```php |
| 187 | +$http_client = new ScientiaMobile\WurflCloud\HttpClient\Curl(); |
| 188 | + |
| 189 | +// Timeout is in milliseconds (10000 == 10 seconds) |
| 190 | +$http_client->setTimeout(10000); |
| 191 | + |
| 192 | +// Pass $http_client in to the Client to use it |
| 193 | +$client = new ScientiaMobile\WurflCloud\Client($config, $cache, $http_client); |
| 194 | +``` |
| 195 | + |
| 196 | +# Unit Testing |
| 197 | +Unit tests are included with the client and can be run with PHPUnit. |
| 198 | +Before you can run the unit tests, you must install the dependencies |
| 199 | +via Composer from the root directory: |
| 200 | + |
| 201 | + cd WurflCloudClient-PHP* |
| 202 | + curl -sS https://getcomposer.org/installer | php |
| 203 | + php composer.phar install |
| 204 | + |
| 205 | +Before you run the PHPUnit tests, you must set your WURFL Cloud API Key |
| 206 | +in an environment variable so it can be used in the tests. To run the |
| 207 | +tests, run `phpunit` from the root directory (where `phpunit.xml.dist` |
| 208 | +is located): |
| 209 | + |
| 210 | + export WURFL_CLOUD_API_KEY="123456:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 211 | + php vendor/phpunit/phpunit/phpunit.php -v |
| 212 | + |
| 213 | +Note that in order to get all the tests to pass, you will need to use |
| 214 | +the API Key from a Premium WURFL Cloud account with all capabilities |
| 215 | +enabled. This is because small responses from the WURFL Cloud system |
| 216 | +are never compressed if they fit within one network packet, and the |
| 217 | +unit tests that cover compression see this as a failure. If this is |
| 218 | +the case, you will see failures like this: |
| 219 | + |
| 220 | + There were 3 failures: |
| 221 | + |
| 222 | + 1) ScientiaMobile\WurflCloud\HttpClient\CurlTest::testCallCompression |
| 223 | + Failed asserting that an array contains 'Content-Encoding: gzip'. |
| 224 | + |
| 225 | + 2) ScientiaMobile\WurflCloud\HttpClient\FsockTest::testCallCompression |
| 226 | + Failed asserting that an array contains 'Content-Encoding: gzip'. |
| 227 | + |
| 228 | + 3) ScientiaMobile\WurflCloud\HttpClient\GuzzleTest::testCallCompression |
| 229 | + Failed asserting that an array contains 'Content-Encoding: gzip'. |
| 230 | + |
| 231 | +You will also need to have the extensions `apc`, `memcache`, `memcached`, |
| 232 | +`json`, and `curl` enabled. By default, `apc` is not enabled in CLI mode, |
| 233 | +so you may need to launch phpunit like this to force it on: |
| 234 | + |
| 235 | + php -d apc.enable_cli=1 vendor/phpunit/phpunit/phpunit.php -v |
| 236 | + |
| 237 | +**2015 ScientiaMobile Incorporated** |
| 238 | + |
| 239 | +**All Rights Reserved.** |
| 240 | + |
| 241 | +**NOTICE**: All information contained herein is, and remains the property of |
| 242 | +ScientiaMobile Incorporated and its suppliers, if any. The intellectual |
| 243 | +and technical concepts contained herein are proprietary to ScientiaMobile |
| 244 | +Incorporated and its suppliers and may be covered by U.S. and Foreign |
| 245 | +Patents, patents in process, and are protected by trade secret or copyright |
| 246 | +law. Dissemination of this information or reproduction of this material is |
| 247 | +strictly forbidden unless prior written permission is obtained from |
| 248 | +ScientiaMobile Incorporated. |
0 commit comments