/
Http.php
executable file
·106 lines (80 loc) · 3.14 KB
/
Http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* ### USE Xlib\Browser ###
* PARA USAR COM SERVLETS, E COMUNICAÇÃO SERVER TO SERVER WEBSERVICE ETC
*/
namespace Xlib;
class Http {
protected $baseUrl = '';
protected $port = '';
protected $connectionTimeOut = 30 ;
protected $timeOut = 40 ;
protected $headers = array ( 'Content-Type: application/json' ) ;
protected $urlHistory = array ( );
/**
*
* @param string $baseUrl
* @param integer $port
*/
public function __construct ( $baseUrl = "" , $port = "" ) {
if ( !empty ( $baseUrl ) ) $this->baseUrl = preg_replace ( '/\/$/' , '' , $baseUrl ) ;
if ( !empty ( $port ) ) $this->port = $port ;
}
/**
* Cria a URL baseada em uma URL base ($this->baseUrl) se houver e porta padrão ($this->port) se houver
* @param string $url
* @return string
* @throws Exception
*/
protected function makeUrl ( $url ) {
if ( !empty ( $this->baseUrl ) && !empty ( $this->port ) ) {
// temos a baseUrl e a porta
$url = $this->baseUrl . ":" . $this->port . DIRECTORY_SEPARATOR . preg_replace ( '/^\//' , '' , $url ) ;
} else if ( !empty ( $this->url ) && empty ( $this->port ) ) {
// temos a baseUrl mas não temos a porta
$url = $this->baseUrl . DIRECTORY_SEPARATOR . preg_replace ( '/^\//' , '' , $url ) ;
} else if ( empty ( $this->url ) && !empty ( $this->port ) ) {
// temos a porta e não temos a baseUrl
throw new Exception ( "baseUrl indefinida" ) ;
}
// não temos nem a baseUrl nem a porta
$this->urlHistory[] = $url ;
return $url ;
}
/**
* Chama uma servlet e devolve uma string
* @param string $url
* @param string $post string para postar Ex.: var1=val1&var2=val2
* @return string
* @throws Exception
*/
public function getAsString ( $url , HttpPost $post = null ) {
$ch = curl_init ( $this->makeUrl ( $url ) ) ;
if ( !empty ( $post ) ) {
curl_setopt ( $ch , CURLOPT_POST , true ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $post->get() ) ;
}
curl_setopt ( $ch , CURLOPT_MUTE , 1 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , 0 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , 0 ) ;
curl_setopt ( $ch , CURLOPT_HTTPHEADER , $this->headers ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ;
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , $this->connectionTimeOut ) ;
curl_setopt ( $ch , CURLOPT_TIMEOUT , $this->timeOut ) ;
$result = curl_exec ( $ch ) ;
if ( curl_errno ( $ch ) ) throw new Exception ( curl_error ( $ch ) , curl_errno ( $ch ) ) ;
curl_close ( $ch ) ;
return $result ;
}
/**
* Chama uma servlet e devolve o objeto
* @param type $url
* @return type
*/
public function get ( $url ) {
return Json::decode ( $this->getAsString ( $url ) );
}
public function __destruct ( ) {
// pr ( $this->urlHistory );
}
}