Skip to content

Commit 2390b6b

Browse files
committed
initial release
0 parents  commit 2390b6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4421
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
Thumbs.db
3+
/vendor

CHANGELOG.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Change log
2+
3+
[2015-05-20] v2.0.2
4+
- Enhancement: Open source cloud client.
5+
6+
[2014-02-13] v2.0.1
7+
- Bugfix: CURL adapter fails with multiple calls to Client::detectDevice()
8+
9+
[2013-10-20] v2.0.0
10+
- Client rewritten with namespaces
11+
- Unit tests added for all classes
12+
- Added support for Guzzle
13+
- Added support for custom HTTP clients
14+
- Added support for SOCKS and HTTP proxy servers
15+
- Added support for Composer
16+
- Improved autoloading
17+
- Bugfix: HTTP timeout is not honored
18+
- Improved documentation
19+
20+
[2012-04-02] v1.0.2
21+
22+
- Bugfix: Strict notices about abstract static methods
23+
- Improved code examples
24+
- Bugfix: Memcache connections are duplicated
25+
26+
[2012-03-16] v1.0.1
27+
28+
- Bugfix: JSON warnings in PHP < 5.3
29+
30+
[2011-12-21] v1.0.0
31+
32+
- Initial release
33+

COPYING.txt

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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.

cache/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "scientiamobile/wurflcloud",
3+
"description": "ScientiaMobile PHP WURFL Cloud Client",
4+
"authors": [{
5+
"name": "Steve Kamerman",
6+
"email": "steve@scientiamobile.com"
7+
}
8+
],
9+
"require": {
10+
"php": ">=5.4.0"
11+
},
12+
"require-dev": {
13+
"guzzle/guzzle": "~3.7@dev",
14+
"phpunit/phpunit": "3.7.*"
15+
},
16+
"autoload": {
17+
"psr-0": {
18+
"ScientiaMobile\\WurflCloud": "src"
19+
}
20+
}
21+
}

examples/MyWurfl.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
use ScientiaMobile\WurflCloud\Config;
3+
use ScientiaMobile\WurflCloud\Cache\APC;
4+
use ScientiaMobile\WurflCloud\Cache\Memcache;
5+
use ScientiaMobile\WurflCloud\Cache\Memcached;
6+
use ScientiaMobile\WurflCloud\Cache\File;
7+
use ScientiaMobile\WurflCloud\Cache\Null;
8+
use ScientiaMobile\WurflCloud\Cache\Cookie;
9+
use ScientiaMobile\WurflCloud\Client;
10+
/**
11+
* WURFL Cloud Singleton
12+
*
13+
* @package WurflCloud_Client
14+
* @subpackage Examples
15+
*/
16+
/**
17+
* WURFL Cloud Singleton
18+
*
19+
* An example of using a single class to easily get WURFL Capabilities.
20+
* Make sure you edit the $api_key in this file.
21+
*
22+
* You can use this class in your scripts like this:
23+
* <code>
24+
* // Include the MyWurfl.php file
25+
* require_once './MyWurfl.php';
26+
* // Get the is_wireless_device capability from the visiting device
27+
* $wireless = MyWurfl::get('is_wireless_device');
28+
* </code>
29+
*
30+
* @package WurflCloud_Client
31+
* @subpackage Example
32+
*/
33+
class MyWurfl {
34+
/**
35+
* Enter your API Key here
36+
* @var string
37+
*/
38+
private static $api_key = '123456:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
39+
/**
40+
* Initialize static instance
41+
*/
42+
private static function init() {
43+
require_once __DIR__.'/../src/autoload.php';
44+
// Additional configuration options can be used here
45+
$config = new Config();
46+
$config->api_key = self::$api_key;
47+
48+
// Set the cache that you'd like to use. Here are some options:
49+
$cache = new Cookie();
50+
//$cache = new APC();
51+
//$cache = new Memcache();
52+
//$cache = new Memcached();
53+
//$cache = new File();
54+
55+
// These two lines setup the Client and do the device detection
56+
self::$instance = new Client($config, $cache);
57+
self::$instance->detectDevice();
58+
}
59+
/**
60+
* Returns the value of the requested capability
61+
* @param string $capability_name
62+
* @return mixed Value of the requested capability
63+
*/
64+
public static function get($capability_name) {
65+
if (self::$instance === null) self::init();
66+
return self::$instance->getDeviceCapability($capability_name);
67+
}
68+
public static function getInstance() {
69+
return self::$instance;
70+
}
71+
/**
72+
* @var WurflCloud_Client_Client
73+
*/
74+
private static $instance;
75+
}

0 commit comments

Comments
 (0)