| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | require_once 'Zend/Service/Abstract.php'; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | class Diggin_Service_Munoh extends Zend_Service_Abstract |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | const API_URL = 'http://m.unoh.net/puchipuchi'; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | protected $_client; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | protected static $_lastRequestTime = 0; |
|---|
| 43 | |
|---|
| 44 | public function getApiUrl() |
|---|
| 45 | { |
|---|
| 46 | return self::API_URL; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | public function puchipuchi(array $parms = null) { |
|---|
| 56 | $response = $this->makeRequest($parms); |
|---|
| 57 | return $this->_xmlResponseToSimpleXMLIterator($response); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | public function makeRequest(array $parms = null) |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | $timeDiff = microtime(true) - self::$_lastRequestTime; |
|---|
| 71 | if ($timeDiff < 1) { |
|---|
| 72 | usleep((1 - $timeDiff) * 1000000); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | $this->_client = self::getHttpClient(); |
|---|
| 76 | $this->_client->setUri(self::getApiUrl()); |
|---|
| 77 | if(isset($parms)){ |
|---|
| 78 | $this->_client->setParameterPOST($parms); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | self::$_lastRequestTime = microtime(true); |
|---|
| 82 | $response = $this->_client->request('POST'); |
|---|
| 83 | |
|---|
| 84 | if (!$response->isSuccessful()) { |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | require_once 'Diggin/Service/Munoh/Exception.php'; |
|---|
| 89 | throw new Diggin_Service_Munoh_Exception("Http client reported an error: '{$response->getMessage()}'"); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | $responseBody = $response->getBody(); |
|---|
| 93 | |
|---|
| 94 | $dom = new DOMDocument() ; |
|---|
| 95 | |
|---|
| 96 | if (!@$dom->loadXML($responseBody)) { |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | require_once 'Diggin/Service/Munoh/Exception.php'; |
|---|
| 101 | throw new Diggin_Service_Munoh_Exception('XML Error'); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | return $dom; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public function getVoice(array $parms = null){ |
|---|
| 108 | $xml = $this->puchipuchi($parms); |
|---|
| 109 | |
|---|
| 110 | return (string) $xml->voice; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | private static function _xmlResponseToSimpleXMLIterator(DOMDocument $response) |
|---|
| 121 | { |
|---|
| 122 | $sxml = simplexml_import_dom($response, 'SimpleXMLIterator'); |
|---|
| 123 | |
|---|
| 124 | return $sxml; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | } |
|---|