/application/libraries/pagseguro/frete.php

https://bitbucket.org/naando_araujo/pagseguro · PHP · 105 lines · 96 code · 8 blank · 1 comment · 13 complexity · 74fbce56d389a9b4b2aa229e735cc77f MD5 · raw file

  1. <?php
  2. class PgsFrete
  3. {
  4. private $_use = 'curl';
  5. private $_debug = false;
  6. private $_methods = array('curl');
  7. private $_result;
  8. public function PgsFrete()
  9. {
  10. if ($this->debug()) {
  11. echo "\nPgsFrete started!";
  12. }
  13. }
  14. public function debug($debug=null)
  15. {
  16. if (null===$debug) {
  17. return $this->_debug;
  18. }
  19. $this->_debug = (bool) $debug;
  20. }
  21. public function setUse($useMethod)
  22. {
  23. if ('string'!==gettype($useMethod)) {
  24. throw new Exception('Method for setUse not allowed.'.
  25. 'Method passed: '.var_export($useMethod, true));
  26. }
  27. $useMethod = strtolower($useMethod);
  28. if (!in_array($useMethod, $this->_methods)) {
  29. throw new Exception('Method for setUse not allowed.'.
  30. 'Method passed: '.var_export($useMethod, true));
  31. }
  32. $this->_use = $useMethod;
  33. if ($this->debug()) {
  34. echo "\nMethod changed to ".strtoupper($useMethod);
  35. }
  36. }
  37. public function getUse()
  38. {
  39. return $this->_use;
  40. }
  41. public function request($url, $post=null)
  42. {
  43. $method = $this->getUse();
  44. if (in_array($method, $this->_methods)) {
  45. $method_name = '_request'.ucWords($method);
  46. if (!method_exists($this, $method_name)) {
  47. throw new Exception("Method $method_name does not exists.");
  48. }
  49. if ($this->debug()) {
  50. echo "\nTrying to get '$url' using ".strtoupper($method);
  51. }
  52. return call_user_func(array($this, $method_name), $url, $post);
  53. } else {
  54. throw new Exception('Method not seted.');
  55. }
  56. }
  57. private function _requestCurl($url, $post=null)
  58. {
  59. $urlkey="URL:".md5("$url POST:$post");
  60. if(isset($_SESSION[$urlkey])){
  61. $this->_result = $_SESSION[$urlkey];
  62. return;
  63. }
  64. $parse = parse_url($url);
  65. $ch = curl_init();
  66. if ('https'===$parse['scheme']) {
  67. // Nao verificar certificado
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  70. }
  71. curl_setopt($ch, CURLOPT_URL, $url); // Retornar o resultado
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Retornar o resultado
  73. if ($post) {
  74. curl_setopt($ch, CURLOPT_POST, true); // Ativa o modo POST
  75. curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Insere os POSTs
  76. }
  77. $result = curl_exec($ch);
  78. curl_close($ch);
  79. $this->_result = $result;
  80. $_SESSION[$urlkey]=$result;
  81. }
  82. public function gerar($CepOrigem, $Peso, $Valor, $Destino)
  83. {
  84. $Peso=str_replace('.',',',$Peso);
  85. $url = "https://pagseguro.uol.com.br/desenvolvedor/simulador_de_frete_calcular.jhtml?postalCodeFrom={$CepOrigem}&weight={$Peso}&value={$Valor}&postalCodeTo={$Destino}";
  86. $this->request($url);
  87. $result = explode('|',$this->_result);
  88. $valores=array();
  89. if($result[0]=='ok'){
  90. $valores['Sedex']=$result[3];
  91. $valores['PAC']=$result[4];
  92. }else{
  93. die($result[1]);
  94. }
  95. return $valores;
  96. }
  97. }