Independente da missão de nossa saga, seja esta de alegria ou desgraça, o Brasil tem que ser defendido!
        05
        08

        Calculando frete via PagSeguro

        class frete
                                        {
                                            private $_use     = 'curl';
                                            private $_debug   = false;
                                            private $_methods = array('curl');
                                            private $_result;

                                            public function PgsFrete()
                                            {
                                                if ($this->debug()) {
                                                    echo "nPgsFrete started!";
                                                }
                                            }

                                            public function debug($debug=null)
                                            {
                                                if (null===$debug) {
                                                    return $this->_debug;
                                                }
                                                $this->_debug = (bool) $debug;
                                            }

                                            public function setUse($useMethod)
                                            {
                                                if ('string'!==gettype($useMethod)) {
                                                    throw new Exception('Method for setUse not allowed.'.
                                                      'Method passed: '.var_export($useMethod, true));
                                                }
                                                $useMethod = strtolower($useMethod);
                                                if (!in_array($useMethod, $this->_methods)) {
                                                    throw new Exception('Method for setUse not allowed.'.
                                                      'Method passed: '.var_export($useMethod, true));
                                                }
                                                $this->_use = $useMethod;
                                                if ($this->debug()) {
                                                    echo "nMethod changed to ".strtoupper($useMethod);
                                                }
                                            }

                                            public function getUse()
                                            {
                                                return $this->_use;
                                            }

                                            public function request($url, $post=null)
                                            {
                                                $method = $this->getUse();
                                                if (in_array($method, $this->_methods)) {
                                                    $method_name = '_request'.ucWords($method);
                                                    if (!method_exists($this, $method_name)) {
                                                      throw new Exception("Method $method_name does not exists.");
                                                    }
                                                    if ($this->debug()) {
                                                        echo "nTrying to get '$url' using ".strtoupper($method);
                                                    }
                                                    return call_user_func(array($this, $method_name), $url, $post);
                                                } else {
                                                    throw new Exception('Method not seted.');
                                                }
                                            }

                                            private function _requestCurl($url, $post=null)
                                            {
                                                $urlkey="URL:".md5("$url POST:$post");
                                                if(isset($_SESSION[$urlkey])){
                                                  $this->_result = $_SESSION[$urlkey];
                                                  return;
                                                }
                                                $parse = parse_url($url);
                                                $ch    = curl_init();
                                                if ('https'===$parse['scheme']) {
                                                    // Nao verificar certificado
                                                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                                                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                                                }
                                                curl_setopt($ch, CURLOPT_URL, $url); // Retornar o resultado
                                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Retornar o resultado
                                                if ($post) {
                                                    curl_setopt($ch, CURLOPT_POST, true); // Ativa o modo POST
                                                    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Insere os POSTs
                                                }
                                                $result = curl_exec($ch);
                                                curl_close($ch);
                                                $this->_result = $result;
                                                $_SESSION[$urlkey]=$result;
                                            }

                                            public function gerar($CepOrigem, $Peso, $Valor, $Destino)
                                            {
                                                $Peso=str_replace('.',',',$Peso);
                                                $url = "https://pagseguro.uol.com.br/desenvolvedor/simulador_de_frete_calcular.jhtml?postalCode={$CepOrigem}&weight={$Peso}&value={$Valor}&postalCodeTo={$Destino}";
                                                $this->request($url);
                                                $result = explode('|',$this->_result);
                                                $valores=array();
                                                if($result[0]=='ok'){
                                                  $valores['Sedex']=$result[3];
                                                  $valores['PAC']=$result[4];
                                                }else{
                                                  die($result[1]);
                                                }
                                                return $valores;
                                            }
                                        }

        Comentários

        Tipo de Posts
        Parceiros
        Feed

        Entre com o seu e-mail:

        Sobre

        Rafael Nonato Ribeiro Ferreira, graduado em Análise e Desenvolvimento de Sistemas pela Uninove, programador nas linguagens PHP, JavaScript, JQuery e Java, desenvolvedor web HTML e CSS desenvolve e modela em SGBDs Mysql, PostGreSQL e Oracle, atualmente trabalha como Analista e Desenvolvedor de Sistemas, é um brasileiro, cristão, patriota filiado ao PSOL e com convicções politicas próprias conforme será mostrado em seu futuro site www.constituicaoproposta.com.br, nas horas de folga gosta de tocar seu violão e compor letras de múisca, Torço para o São Paulo e para a Portela.

        © Copyright 2012 NRF Informática Soluções. All Rights Reserved.