Skip to content

Commit

Permalink
Merge pull request #3 from ghazanfarmir/develop
Browse files Browse the repository at this point in the history
Improved wrapper class with more methods to Companies House resources
  • Loading branch information
GhazanfarMir authored Aug 30, 2017
2 parents c3f13ea + fef8254 commit ff9e4e0
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 80 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ sudo: required

language: php

cache:
directories:
- $HOME/.composer/cache

env:
- PLATFORM=travis

php:
- 7.1

Expand Down
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ More information about this free API can be found

## Installation

To install, use the following to pull the package via `Composer`.
To install, use the following to pull the package via Composer.

```
composer require ghazanfar/laravel-companies-house
composer require ghazanfarmir/laravel-companies-house
```

Now register the Service provider in `config/app.php`
Now register the Service Provider in config/app.php

```
'providers' => [
...
Ghazanfar\CompaniesHouse\CompaniesHouseServiceProvider::class,
Expand All @@ -28,6 +29,7 @@ And also add the alias to the same file.

```
'aliases' => [
...
'CompaniesHouse' => Ghazanfar\CompaniesHouse\Facades\CompaniesHouse::class,
Expand All @@ -36,18 +38,52 @@ And also add the alias to the same file.

## How to use?

#### Company

```
Put instructions on how to use the package
// search companies by name
CompaniesHouseApi::company()->search('Company name');
```

`CompaniesHouseApi::company()->find('company number')` is now going to return an **object** which can be chained to get other resources related data.

For example, calling `get()` on the object will return company information:

```
CompaniesHouseApi::company()->search('Company name'); // search companies by name
// find companies by number
CompaniesHouseApi::company()->find('company number')->get();
```

Similarly, you may get information related to other company resources such offices, company_registered_office, filing_history etc using the helper method **with()** which accepts an array of resources you need.

**Example**

```
CompaniesHouseApi::company()->find('company number'); // find companies by number
$obj = CompaniesHouseApi::company()->with('officers')->find('company_number');
$company = $obj->get();
$officers = $obj->officers();
...
```

You may include more than one resources in **with()** within an array as follows:

```
$obj = CompaniesHouseApi::company()->with(['officers', 'registered_office_address'])->find('company_number');
$officers = $obj->officers(); // get company's officers collection
$registered_address = $obj->registeredOfficeAddress(); // get company's registered address
```

#### Officers

```
CompaniesHouseApi::officers()->search('Mir'); // search officers by name
```
Expand Down
31 changes: 22 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@

use Ghazanfar\CompaniesHouseApi\CompaniesHouseApi;
use Ghazanfar\CompaniesHouseApi\Http\Client;
use Ghazanfar\CompaniesHouseApi\Resources\Officers;

include 'vendor/autoload.php';

try {

$api = new CompaniesHouseApi(new Client());
$base_uri = 'https://api.companieshouse.gov.uk/';

$all = $api->company()->searchAll('Ebury Partners', 1, 2);
$api_key = 'IvSp6uE13FPbE8iDPx6Yey9aQ64jH3Cvm18eAE_N';

$client = new Client($base_uri, $api_key);

$api = new CompaniesHouseApi($client);



$all = $api->company()->searchAll('Ebury Partners');

$companies = $api->company()->search('Ebury Partners');

$company = $api->company()->find('07086058');
$company = $api->company()->find('07086058')->get();

$officers = $api->officers()->search('Mir');

Expand All @@ -33,15 +42,19 @@
// Search Officers
print_r('Search Officers: ' . $officers->items[0]->title . PHP_EOL);

// Search by number
print_r('Search Company byNumber: ' . $company->company_name . PHP_EOL);

// Search Officers
print_r('Search Officers: ' . $officers->items[0]->title . PHP_EOL);

// Search Disqualified Officers
print_r('Search Disqualified Officers: ' . $disqualified->items[0]->title . PHP_EOL);

// company officers
print_r('Show Company officers: ' . $api->company()->with(['officers'])->find('07086058')->officers()->items[0]->name . PHP_EOL);




$o = $api->company()->with(array('officers', 'charges', 'registered_office_address'))->find('07086058');



} catch (Exception $e) {
echo $e->getMessage();
}
9 changes: 1 addition & 8 deletions src/CompaniesHouseApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public function register()

$api_key = 'IvSp6uE13FPbE8iDPx6Yey9aQ64jH3Cvm18eAE_N';

/*$client = new Client(
array(
'base_uri' => $base_uri,
'auth' => array($api_key, '')
)
);*/

$client = new Client();
$client = new Client($base_uri, $api_key);

return new CompaniesHouseApi($client);
});
Expand Down
171 changes: 152 additions & 19 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,93 @@ class Client
{

/**
* API KEY used for testing
* @var string
*/

const API_KEY = 'IvSp6uE13FPbE8iDPx6Yey9aQ64jH3Cvm18eAE_N';
protected $api_key;

/**
* BASE API
* @var string
*/
const BASE_URI = 'https://api.companieshouse.gov.uk/';
protected $base_uri;

/**
* @var resource
*/
protected $curl;
protected $handle;

/**
* @var
*/
protected $options;

/**
* @var
*/
protected $uri;

/**
* @var
*/
protected $response;

/**
* Client constructor.
* @param $base_uri
* @param $api_key
* @internal param $options
*/
public function __construct($base_uri, $api_key)
{
$this->base_uri = $base_uri;

$this->api_key = $api_key;
}

/**
* @return string
*/
public function getBaseUri()
{
return $this->base_uri;
}

/**
* @param string $base_uri
*/
public function setBaseUri($base_uri)
{
$this->base_uri = $base_uri;
}


/**
* @param $name
* @param $value
*/
public function setOption($name, $value)
{
curl_setopt($this->handle, $name, $value);
}

/**
* @return mixed
*/
public function getOptions()
{
return $this->options;
}

/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = $options;
curl_setopt_array($this->handle, $options);

return $this;
}

/**
* @param $uri
Expand All @@ -33,28 +106,88 @@ class Client
public function get($uri, $params = null)
{

$queryString = '';
try {

$url = $this->buildUrl($uri, $params);

$this->handle = curl_init($url);

$this->setOptions(array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->api_key . ":",
CURLOPT_RETURNTRANSFER => 1
));

$this->execute();

$this->close();

return $this->getResponse();

} catch (\Exception $e) {
echo $e->getMessage();
}
}

/**
* @param $uri
* @param $params
* @return string
*/
public function buildUrl($uri, $params)
{

if (isset($params) && count($params)) {

if (isset($params)) {
$queryString = http_build_query($params);

return sprintf("%s%s?%s", $this->base_uri, $uri, $queryString);
}

$url = sprintf("%s%s?%s", self::BASE_URI, $uri, $queryString);
return sprintf("%s%s", $this->base_uri, $uri);
}

// initialise curl
$this->curl = curl_init($url);
/**
* @return mixed
* @throws \Exception
*/
public function execute()
{

// Optional Authentication:
curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($this->curl, CURLOPT_USERPWD, self::API_KEY . ":");
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($this->handle);

if (false === ($response = curl_exec($this->curl))) {
echo 'Curl error: ' . curl_error($this->curl);
if (CURLE_OK !== $this->getErrorCode()) {
throw new \Exception(
sprintf('An error (%d) occurred while executing the cURL request.', $this->getErrorCode())
);
}
$this->response = $response;

curl_close($this->curl);
return $this;
}

return $response;
/**
* @return int
*/
public function getErrorCode()
{
return curl_errno($this->handle);
}

/**
* @return mixed
*/
public function getResponse()
{
return $this->response;
}

/**
* close Curl
*/
public function close()
{
curl_close($this->handle);
}

}
Loading

0 comments on commit ff9e4e0

Please sign in to comment.