<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>REST &#8211; Mach3.laBlog</title>
	<atom:link href="https://blog.mach3.jp/tag/rest/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.mach3.jp</link>
	<description></description>
	<lastBuildDate>Tue, 18 Oct 2011 05:31:42 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>PEARのServices_Twitterで未定義のエンドポイントを利用する</title>
		<link>https://blog.mach3.jp/2011/10/18/add-endpoints-to-services_twitte.html</link>
		
		<dc:creator><![CDATA[mach3]]></dc:creator>
		<pubDate>Tue, 18 Oct 2011 05:31:42 +0000</pubDate>
				<category><![CDATA[Laboratory]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Services_Twitter]]></category>
		<category><![CDATA[Twitter]]></category>
		<guid isPermaLink="false">http://blog.mach3.jp/?p=2082</guid>

					<description><![CDATA[この記事の情報は古くなっています。 Twitter APIのバージョンも上がり、Services_Twitterは現在メンテナンスされていない様です。 「Services_Twitter」は、簡単にTwitterAPIを [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="caution">
    この記事の情報は古くなっています。<br />
    Twitter APIのバージョンも上がり、Services_Twitterは現在メンテナンスされていない様です。
</p>



<p>「<a href="http://pear.php.net/package/Services_Twitter">Services_Twitter</a>」は、簡単にTwitterAPIを利用する為のPEARパッケージです。<br />
今回の記事は、このパッケージでサポートされていないエンドポイントを使う方法の備忘録です。</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh4.googleusercontent.com/-gw3047B-bIA/Tp0OnuC4ktI/AAAAAAAABYA/5E_EXhAF310/s400/201110181428.png" alt="PEARのServices_Twitterで未定義のエンドポイントを利用する"/></figure>



<p></p>



<span id="more-2082"></span>



<h2 class="wp-block-heading">Services_Twitterで利用できない物</h2>



<p>例えば、<em>statuses/following_timeline</em>や、<em>statuses/media_timeline</em>等、<br />
公式のREST APIドキュメントに記載されていないものは、<br />
初期状態のServices_Twitterでは使用する事ができません。</p>



<ul class="wp-block-list">
<li><a href="https://dev.twitter.com/docs/api">REST API Resources | Twitter Developers</a></li>
</ul>



<p>※公式でアナウンスされていない物を使うわけですから、使用は勿論自己責任になります。</p>



<h2 class="wp-block-heading">Services_Twitterにエンドポイントを追加する方法</h2>



<p>Services_Twitterは、XMLで使用できるエンドポイントを管理していて、<br />
そこに定義されていない物を呼びだそうとすると例外を吐く作りになっています。</p>



<h3 class="wp-block-heading">api.xmlの所在</h3>



<p>エンドポイントの定義ファイルである「api.xml」は、<br />
PEARのインストールディレクトリ内の</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>data/Services_Twitter/data/api.xml</p>
</blockquote>



<p>に置いてあります。<br />
これに<endpoint>を追加してやれば、エンドポイントを使えるようになる仕組み。<br />
ただ、これを直接編集するのもちょっとアレだなぁと思い…</endpoint></p>



<h2 class="wp-block-heading">XMLを指定してエンドポイントを追加出来るように拡張してみる</h2>



<p>自前のxmlファイルのパスを引数に突っ込んで、<br />
未定義のエンドポイントを足せるようにServices_Twitterクラスを拡張してみます。</p>



<h3 class="wp-block-heading">書いてみた物</h3>



<ul class="wp-block-list">
<li><a href="https://gist.github.com/1292371">Servces_Twitterを拡張して定義ファイルを追加で読み込めるようにする。 — Gist</a></li>
</ul>



<pre class="wp-block-preformatted"><!--?php
require_once( "Services/Twitter.php" );

class MY_Services_twitter extends Services_twitter {

    public function __construct(){
        parent::__construct();
    }

    public function addAPI( $xml ){
        if( !file_exists( $xml ) ){
            throw new Exception( "'{$xml}' is not found." );
        }
        $xmlApi = simplexml_load_file( $xml );
        foreach ($xmlApi--->category as $category) {
            $catName = (string)$category['name'];
            if( !is_array( $this-&gt;api[$catName] ) ){
                $this-&gt;api[$catName] = array();
            }
            foreach ($category-&gt;endpoint as $endpoint) {
                $this-&gt;api[$catName][(string)$endpoint['name']] = $endpoint;
            }
        }
    }
}
</pre>



<p>うん、ものすごくたいした拡張ではないですね。<br />
使い方は、addAPIメソッドにXMLへのパスを突っ込むだけ。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>

$foo = <span class="hljs-keyword">new</span> MY_Services_twitter();
$foo-&gt;addAPI( <span class="hljs-string">"/the/path/to/your/api.xml"</span> );
</span></code></span></pre>


<h2 class="wp-block-heading">試しにエンドポイントを追加してみたXML</h2>



<p>試しにfollowing_timelinとmedias_timelineを追加する為のXMLを記述してみます。</p>



<p>仕様は未公開のうえ、パラメータの試験とかはやってないので、<br />
繰り返しますがご使用は自己責任で。<br />
以下の例のパラメータは、公式のWebサイトで使われている物を<br />
それっぽく適当に突っ込んでいます。</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">api</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">category</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"statuses"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">endpoint</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"following_timeline"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"GET"</span> <span class="hljs-attr">auth_required</span>=<span class="hljs-string">"false"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">formats</span>&gt;</span>xml,json,rss,atom<span class="hljs-tag">&lt;/<span class="hljs-name">formats</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"include_entities"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"include_available_feature"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"contributor_details"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"user_id"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"id_or_screenname"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"screen_name"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"id_or_screenname"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">endpoint</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">endpoint</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"media_timeline"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"GET"</span> <span class="hljs-attr">auth_required</span>=<span class="hljs-string">"false"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">formats</span>&gt;</span>xml,json,rss,atom<span class="hljs-tag">&lt;/<span class="hljs-name">formats</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"offset"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"integer"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"count"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"integer"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"score"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"filter"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"include_entities"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"boolean"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"user_id"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"id_or_screenname"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">param</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"screen_name"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"id_or_screenname"</span> <span class="hljs-attr">required</span>=<span class="hljs-string">"false"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">endpoint</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">category</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">api</span>&gt;</span>
</code></span></pre>


<h2 class="wp-block-heading">まとめ</h2>



<p>Services_Twitterではエンドポイントへのアクセスを__callで実装していて、<br />
目新しかったので参考になりました。<br />
今度機会があったら使ってみよう。オーバーロード。</p>



<ul class="wp-block-list">
<li><a href="http://php.net/manual/ja/language.oop5.overloading.php">PHP: オーバーロード &#8211; Manual</a></li>
</ul>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
