Skip to content

Commit 44e964c

Browse files
committed
Added connection type property: curl (default) | file_get_content
1 parent 41ff3c8 commit 44e964c

3 files changed

Lines changed: 96 additions & 20 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,23 @@ https://github.com/lis-dev/nova-poshta-api-2/archive/master.zip
4545
```php
4646
$np = new NovaPoshtaApi2('Ваш_ключ_API_2.0');
4747
```
48+
49+
## Создание экземпляра класса (с расширенными параметрами)
50+
Рекомендуется использовать, если необходимо получать данные на языке, отличном от русского, выбрасывать Exception при ошибке запроса, или при отсутствии установленной библиотеки curl на сервере
51+
```php
52+
$np = new NovaPoshtaApi2(
53+
'Ваш_ключ_API_2.0',
54+
'ru', // Язык возвращаемых данных: ru (default) | ua | en
55+
FALSE, // При ошибке в запросе выбрасывать Exception: FALSE (default) | TRUE
56+
'curl' // Используемый механизм запроса: curl (defalut) | file_get_content
57+
);
58+
```
59+
4860
## Получение информации о трек-номере
4961
```php
5062
$result = $np->documentsTracking('59000000000000');
5163
```
64+
5265
## Получение сроков доставки
5366
```php
5467
// Получение кода города по названию города и области

src/Delivery/NovaPoshtaApi2.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class NovaPoshtaApi2 {
3232
*/
3333
protected $language = 'ru';
3434

35+
/**
36+
* @var string $connectionType Connection type (curl | file_get_contents)
37+
*/
38+
protected $connectionType = 'curl';
39+
3540
/**
3641
* @var string $areas Areas (loaded from file, because there is no so function in NovaPoshta API 2.0)
3742
*/
@@ -58,13 +63,15 @@ class NovaPoshtaApi2 {
5863
* @param string $key NovaPoshta API key
5964
* @param string $language Default Language
6065
* @param bool $throwErrors Throw request errors as Exceptions
66+
* @param bool $connectionType Connection type (curl | file_get_contents)
6167
* @return NovaPoshtaApi2
6268
*/
63-
function __construct($key, $language = 'ru', $throwErrors = FALSE) {
69+
function __construct($key, $language = 'ru', $throwErrors = FALSE, $connectionType = 'curl') {
6470
$this->throwErrors = $throwErrors;
6571
return $this
6672
->setKey($key)
6773
->setLanguage($language)
74+
->setConnectionType($connectionType)
6875
->model('Common');
6976
}
7077

@@ -88,6 +95,26 @@ function getKey() {
8895
return $this->key;
8996
}
9097

98+
/**
99+
* Setter for $connectionType property
100+
*
101+
* @param string $connectionType Connection type (curl | file_get_contents)
102+
* @return this
103+
*/
104+
function setConnectionType($connectionType) {
105+
$this->connectionType = $connectionType;
106+
return $this;
107+
}
108+
109+
/**
110+
* Getter for $connectionType property
111+
*
112+
* @return string
113+
*/
114+
function getConnectionType() {
115+
return $this->connectionType;
116+
}
117+
91118
/**
92119
* Setter for language property
93120
*
@@ -191,15 +218,26 @@ private function request($model, $method, $params = NULL) {
191218
? $this->array2xml($data)
192219
: $post = json_encode($data);
193220

194-
$ch = curl_init($url);
195-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
196-
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.($this->format == 'xml' ? 'text/xml' : 'application/json')));
197-
curl_setopt($ch, CURLOPT_HEADER, 0);
198-
curl_setopt($ch, CURLOPT_POST, 1);
199-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
200-
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
201-
$result = curl_exec($ch);
202-
curl_close($ch);
221+
if ($this->getConnectionType() == 'curl') {
222+
$ch = curl_init($url);
223+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
224+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: '.($this->format == 'xml' ? 'text/xml' : 'application/json')));
225+
curl_setopt($ch, CURLOPT_HEADER, 0);
226+
curl_setopt($ch, CURLOPT_POST, 1);
227+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
228+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
229+
$result = curl_exec($ch);
230+
curl_close($ch);
231+
} else {
232+
$result = file_get_contents($url, null, stream_context_create(array(
233+
'http' => array(
234+
'method' => 'POST',
235+
'header' => "Content-type: application/x-www-form-urlencoded;\r\n",
236+
'content' => $post,
237+
),
238+
)));
239+
}
240+
203241
return $this->prepare($result);
204242
}
205243

tests/NovaPoshtaApi2Test.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ function setUp() {
3333
// Create new instance
3434
$this->np = new NovaPoshtaApi2($this->key);
3535
}
36+
37+
/**
38+
* Test connectin via file_get_contents()
39+
*/
40+
function testSetConnectionType() {
41+
$result = $this->np->setConnectionType('file_get_contents');
42+
$this->assertInstanceOf('LisDev\Delivery\NovaPoshtaApi2', $result);
43+
}
44+
45+
/**
46+
* getConnectionType()
47+
*/
48+
function testGetConnectionType() {
49+
$result = $this->np->getConnectionType();
50+
$this->assertNotEmpty($result);
51+
}
52+
3653
/**
3754
* getKey()
3855
*/
@@ -57,6 +74,14 @@ function testDocumentsTrackingResultArray() {
5774
$this->assertTrue($result['success']);
5875
}
5976

77+
/**
78+
* Test request via file_get_content
79+
*/
80+
function testRequestViaFileGetContent() {
81+
$result = $this->np->setConnectionType('file_get_content')->documentsTracking('59000082032106');
82+
$this->assertTrue($result['success']);
83+
}
84+
6085
/**
6186
* documentsTracking() result in json
6287
*/
@@ -493,16 +518,6 @@ function testGetDocumentList($params = NULL) {
493518
return $result['data'][0]['Ref'];
494519
}
495520

496-
/**
497-
* getDocument()
498-
*
499-
* @depends testGetDocumentList
500-
*/
501-
function testGetDocument($ref) {
502-
$result = $this->np->getDocument($ref);
503-
$this->assertTrue($result['success']);
504-
}
505-
506521
/**
507522
* generateReport()
508523
*/
@@ -566,6 +581,16 @@ function testNewInternetDocument($sender) {
566581
return $result['data'][0]['Ref'];
567582
}
568583

584+
/**
585+
* getDocument()
586+
*
587+
* @depends testNewInternetDocument
588+
*/
589+
function testGetDocument($ref) {
590+
$result = $this->np->getDocument($ref);
591+
$this->assertTrue($result['success']);
592+
}
593+
569594
/**
570595
* printDocument()
571596
*

0 commit comments

Comments
 (0)