root/lang/php/Diggin_Service_Munoh/Munoh.php

Revision 8588, 3.6 kB (checked in by sasezaki, 5 months ago)

getVoiceメソッドを追加

Line 
1<?php
2/**
3 *
4 * how to use *
5 * $munoh = new Diggin_Service_Munoh();
6 * var_dump($munoh->puchipuchi(array("action"=>"arai")));
7 *
8 * $munoh = new Diggin_Service_Munoh();
9 * echo ($munoh->getVoice());
10 *
11 */
12
13/**
14 * @see Zend_Service_Abstract
15 */
16require_once 'Zend/Service/Abstract.php';
17
18/**
19 * @category   Diggin
20 * @package    Diggin_Service_Munoh
21 * @subpackage Munoh
22 * @author     sasezaki
23 */
24
25class Diggin_Service_Munoh extends Zend_Service_Abstract
26{
27   
28    const API_URL = 'http://m.unoh.net/puchipuchi';
29
30    /**
31     * Zend_Http_Client Object
32     *
33     * @var Zend_Http_Client
34     */
35    protected $_client;
36   
37    /**
38     * Microtime of last request
39     *
40     * @var float
41     */
42    protected static $_lastRequestTime = 0;
43   
44    public function getApiUrl()
45    {
46        return self::API_URL;
47    }
48   
49    /**
50     * puchipuchi!
51     *
52     * @param array
53     * @return SimpleXMLIterator
54     */
55    public function puchipuchi(array $parms = null) {
56        $response = $this->makeRequest($parms);
57        return $this->_xmlResponseToSimpleXMLIterator($response);
58    }
59   
60    /**
61     * Handles all POST requests to a web service
62     *
63     * @param  array  $parms Array of POST parameters
64     * @return mixed  decoded response from web service
65     */
66    public function makeRequest(array $parms = null)
67    {
68        // if previous request was made less then 1 sec ago
69        // wait until we can make a new request
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              * @see Diggin_Service_Munoh_Exception
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            * @see Diggin_Service_Tumblr_Exception
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     * Transform XML string to SimpleXMLIterator
115     *
116     * @param  DOMDocument $response
117     * @return SimpleXMLIterator
118     */
119//    private static function _xmlResponseToPostArray(DOMDocument $response)
120    private static function _xmlResponseToSimpleXMLIterator(DOMDocument $response)
121    {
122        $sxml = simplexml_import_dom($response, 'SimpleXMLIterator');
123               
124        return $sxml;
125    }
126   
127//    private function _xmlIteration(SimpleXMLIterator $obj)
128//    {
129//        foreach ($obj as $key => $value) {
130//            $ret['key'] = $key;
131//            $ret['value'] = $value;
132//            if($obj->hasChildren()) {
133//                $this->_xmlIteration($obj->getChildren());                   
134//            }
135//        }
136//       
137//        return;
138//    }
139}
Note: See TracBrowser for help on using the browser.