/src/SOSTheBlack/Moip/Client.php

https://gitlab.com/fabiorf/moip-1 · PHP · 133 lines · 80 code · 20 blank · 33 comment · 21 complexity · fa65b7ec07bb747a8f163b0970c57062 MD5 · raw file

  1. <?php namespace SOSTheBlack\Moip;
  2. use App;
  3. /**
  4. * MoIP's API connection class
  5. *
  6. * @author Herberth Amaral
  7. * @author Paulo Cesar
  8. * @version 0.0.2
  9. * @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>
  10. */
  11. class Client {
  12. /**
  13. * Method send()
  14. *
  15. * Send the request to API's server
  16. *
  17. * @param string $credentials Token and key to the authentication
  18. * @param string $xml The XML request
  19. * @param string $url The server's URL
  20. * @param string $method Method used to send the request
  21. * @throws Exception
  22. * @return \SOSTheBlack\Moip\Response
  23. */
  24. public function send($credentials, $xml, $url='https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica', $method='POST') {
  25. $header[] = "Authorization: Basic " . base64_encode($credentials);
  26. if (!function_exists('curl_init')){
  27. throw new Exception('This library needs cURL extension');
  28. }
  29. $curl = curl_init();
  30. curl_setopt($curl, CURLOPT_URL, $url);
  31. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  32. curl_setopt($curl, CURLOPT_USERPWD, $credentials);
  33. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  34. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
  35. if ($method == 'POST') curl_setopt($curl, CURLOPT_POST, true);
  36. if ($xml != '') curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
  37. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  38. $ret = curl_exec($curl);
  39. $err = curl_error($curl);
  40. curl_close($curl);
  41. return App::make('\SOSTheBlack\Moip\Response', [['resposta' => $ret, 'erro' => $err]]);
  42. }
  43. /**
  44. * @param string $credentials token / key authentication Moip
  45. * @param string $xml url request
  46. * @param string $url url request
  47. * @param string $error errors
  48. * @return \SOSTheBlack\Moip\Response
  49. */
  50. function curlPost($credentials, $xml, $url, $error=null) {
  51. if (!$error) {
  52. $header[] = "Expect:";
  53. $header[] = "Authorization: Basic " . base64_encode($credentials);
  54. $ch = curl_init();
  55. $options = array(CURLOPT_URL => $url,
  56. CURLOPT_HTTPHEADER => $header,
  57. CURLOPT_SSL_VERIFYPEER => false,
  58. CURLOPT_POST => true,
  59. CURLOPT_POSTFIELDS => $xml,
  60. CURLOPT_RETURNTRANSFER => true,
  61. CURLINFO_HEADER_OUT => true
  62. );
  63. curl_setopt_array($ch, $options);
  64. $ret = curl_exec($ch);
  65. $err = curl_error($ch);
  66. $info = curl_getinfo($ch);
  67. curl_close($ch);
  68. if ($info['http_code'] == "200")
  69. return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]);
  70. elseif ($info['http_code'] == "500")
  71. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]);
  72. elseif ($info['http_code'] == "401")
  73. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]);
  74. else
  75. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]);
  76. } else {
  77. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]);
  78. }
  79. }
  80. /**
  81. * @param string $credentials token / key authentication Moip
  82. * @param string $url url request
  83. * @param string $error errors
  84. * @return \SOSTheBlack\Moip\Response
  85. */
  86. function curlGet($credentials, $url, $error=null) {
  87. if (!$error) {
  88. $header[] = "Expect:";
  89. $header[] = "Authorization: Basic " . base64_encode($credentials);
  90. $ch = curl_init();
  91. $options = array(CURLOPT_URL => $url,
  92. CURLOPT_HTTPHEADER => $header,
  93. CURLOPT_SSL_VERIFYPEER => false,
  94. CURLOPT_RETURNTRANSFER => true
  95. );
  96. curl_setopt_array($ch, $options);
  97. $ret = curl_exec($ch);
  98. $err = curl_error($ch);
  99. $info = curl_getinfo($ch);
  100. curl_close($ch);
  101. if ($info['http_code'] == "200")
  102. return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]);
  103. else if ($info['http_code'] == "500")
  104. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]);
  105. else if ($info['http_code'] == "401")
  106. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]);
  107. else
  108. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]);
  109. } else {
  110. return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]);
  111. }
  112. }
  113. }