|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Classe genérica de interação com webservice |
| 5 | + * |
| 6 | + * @package Crphp |
| 7 | + * @subpackage webservice |
| 8 | + * @author Fábio J L Ferreira <contato@fabiojanio.com> |
| 9 | + * @license MIT (consulte o arquivo license disponibilizado com este pacote) |
| 10 | + * @copyright (c) 2016, Fábio J L Ferreira |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Crphp\Webservice; |
| 14 | + |
| 15 | +class ClienteGenerico |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Apontamento para uma instância de Curl |
| 19 | + * |
| 20 | + * @access private |
| 21 | + * @var object |
| 22 | + */ |
| 23 | + private $curl; |
| 24 | + |
| 25 | + /** |
| 26 | + * Armazena o xml retornado pela consulta |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + private $xml; |
| 31 | + |
| 32 | + /** |
| 33 | + * Armazena as informações referentes a requisição |
| 34 | + * |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + private $info; |
| 38 | + |
| 39 | + /** |
| 40 | + * Atribui alguns valores considerados padrão |
| 41 | + * |
| 42 | + * @access public |
| 43 | + */ |
| 44 | + public function __construct() |
| 45 | + { |
| 46 | + $this->curl = curl_init(); |
| 47 | + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
| 48 | + curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
| 49 | + curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Atribui a URL alvo e o tempo máximo de espera |
| 54 | + * |
| 55 | + * @param string $url |
| 56 | + * @param int $timeout |
| 57 | + */ |
| 58 | + public function setURL($url, $timeout = 30) |
| 59 | + { |
| 60 | + curl_setopt($this->curl, CURLOPT_URL, $url); |
| 61 | + curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Define o agente a ser utilizado |
| 66 | + * |
| 67 | + * @param string $agente |
| 68 | + */ |
| 69 | + public function setAgent($agente = "PHP ClienteGenerico") |
| 70 | + { |
| 71 | + curl_setopt($this->curl, CURLOPT_USERAGENT, $agente); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Adiciona o conteúdo e atribui um cabeçalho a requisição |
| 76 | + * |
| 77 | + * @param string $post |
| 78 | + * @param array $header |
| 79 | + */ |
| 80 | + public function setRequest($post = null, array $header = null) |
| 81 | + { |
| 82 | + if (!$header) |
| 83 | + { |
| 84 | + $header = array( |
| 85 | + "Content-type: text/xml;charset=UTF-8", |
| 86 | + "Accept: text/xml", |
| 87 | + "Cache-Control: no-cache", |
| 88 | + "Pragma: no-cache", |
| 89 | + "Content-length: " . strlen($post), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); |
| 94 | + curl_setopt($this->curl, CURLOPT_POST, true); |
| 95 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Define regras de redirecionamento de URL |
| 100 | + * |
| 101 | + * @param bool $redirect |
| 102 | + * @param int $numRedirect |
| 103 | + * @param bool $refresh |
| 104 | + */ |
| 105 | + public function setRedirect($redirect = true, $numRedirect = 5, $refresh = true) |
| 106 | + { |
| 107 | + curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $redirect); |
| 108 | + curl_setopt($this->curl, CURLOPT_MAXREDIRS, $numRedirect); |
| 109 | + curl_setopt($this->curl, CURLOPT_AUTOREFERER, $refresh); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Executa a consulta a URL alvo |
| 114 | + * |
| 115 | + * @return void|string em caso de sucesso retorna vazio, para erro retorna string |
| 116 | + */ |
| 117 | + public function run() |
| 118 | + { |
| 119 | + $this->xml = curl_exec($this->curl); |
| 120 | + $this->info = curl_getinfo($this->curl); |
| 121 | + curl_close($this->curl); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Informações da requisição obtidos do método curl_getinfo |
| 126 | + * |
| 127 | + * @see http://php.net/manual/pt_BR/function.curl-getinfo.php |
| 128 | + * |
| 129 | + * @return array |
| 130 | + */ |
| 131 | + public function getInfo() |
| 132 | + { |
| 133 | + return $this->info; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Retorna o output devolvido pelo servidor alvo |
| 138 | + * |
| 139 | + * @return void|string em caso de sucesso retorna vazio, para erro retorna string |
| 140 | + */ |
| 141 | + public function getResponse() |
| 142 | + { |
| 143 | + if($this->getInfo()['http_code'] === 500 || $this->getInfo()['http_code'] === 404) |
| 144 | + { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + return $this->xml; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Formata o retorno do método getResponse |
| 153 | + * |
| 154 | + * @return null|string se não tiver dado para transformação retorna null |
| 155 | + */ |
| 156 | + public function formatarXML() |
| 157 | + { |
| 158 | + if(!$this->getResponse()) { return null; } |
| 159 | + |
| 160 | + $dom = new \DOMDocument; |
| 161 | + $dom->preserveWhiteSpace = false; |
| 162 | + $dom->formatOutput = true; |
| 163 | + $dom->loadXML($this->getResponse()); |
| 164 | + |
| 165 | + return htmlentities($dom->saveXML()); |
| 166 | + } |
| 167 | +} |
0 commit comments